|
|
 |
BOOK: Professional C++  | This is the forum to discuss the Wrox book Professional C++ by Nicholas A. Solter, Scott J. Kleper; ISBN: 9780764574849 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional C++ section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

October 17th, 2007, 01:40 PM
|
|
Registered User
|
|
Join Date: Oct 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Static methods
From page 200 of [u]Professional C++</u> comes the following sentences:
"In fact a static method is just like a regular function. The only difference is that it can access private and protected static data members of the class and private and protected non-static data members on other objects of the same type."
Is the last sentence entirely true? It is my belief that a static method only has access to static data members and therefore has no access to non-static data member. Please clarify.
Thank you.
|

March 11th, 2008, 07:29 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Location: San Francisco, CA, .
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry for the delay in replying. I think the confusion comes from the fact that inside the static method, there is no "this" so you wouldn't think that accessing non-static data would make any sense. But you're still inside the class so you can access private and protected data members on *other* objects:
std::cout << someInstance.privateDataMember << std::endl;
----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
|

October 17th, 2008, 02:07 AM
|
|
Authorized User
|
|
Join Date: Oct 2008
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
We had a blast writing this book because there are so many intricacies to C++. Some language features border on the ridiculous, which means that they make great interview questions.
Let's say you have two classes, Super and Sub.
Ex.
Sub is a subclass of Super and it overrides the method foo() as shown below:
class Super
{
public:
virtual void foo(int i);
};
class Sub : public Super
{
public:
virtual void foo(int i);
};
================================================== ======
Victor
Our mission is to provide high quality end to end solutions to the BPO segment in a manner that will improve the operational efficiency while reducing the cost of the services to the client.
4thdimension1@gmail.com
|

October 17th, 2008, 04:30 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 Posts
|
|
So what was the point of that example????
But Sub and Super are abstract classes? Is that what you were pointing out???
Where is the question there???
|

February 28th, 2009, 06:46 PM
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Questioned
The methods have not been implemented.
C++ has a very "rich" syntax. Part of this is inherited from C. For example the notation for defining function pointers(  in my interview). Some is new to C++ like the various uses of the virtual keyword.
How many ways can virtual be used to obtain the same result?

How many ways can const be used and mean the same thing?

|

March 12th, 2009, 02:11 AM
|
|
Registered User
|
|
Join Date: Mar 2009
Location: Shanghai China
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You can declare an object in the static method,
and you have access to private or protected members of the object.
|

March 12th, 2009, 10:39 AM
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
If you are asking about how one class can gain access to private members of another here is an example. Class C1 declares class C2 as a friend.
Code:
class C1
{
friend class C2;
private:
int private_int;
protected:
int protected_int;
};
class C2
{
public:
static void method()
{
C1 c1;
c1.private_int = 1;
c1.protected_int = 2;
}
};
|

March 12th, 2009, 03:16 PM
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Here is an example of a private method of a class being called from within a private method of the same type of class. I do believe this is legal code. A class can access private members of objects of the same class.
Code:
#include<iostream>
using namespace std;
class A
{
public:
A()
{
method();
}
private:
static void method()
{
static int cnt = 0;
if(42 == cnt++)
return;
cout << "count: " << cnt << endl;
A::method();
}
};
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |