Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 17th, 2007, 01:40 PM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

Reply With Quote
  #2 (permalink)  
Old 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
Default

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)
Reply With Quote
  #3 (permalink)  
Old October 17th, 2008, 02:07 AM
Authorized User
 
Join Date: Oct 2008
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Reply With Quote
  #4 (permalink)  
Old October 17th, 2008, 04:30 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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???
Reply With Quote
  #5 (permalink)  
Old February 28th, 2009, 06:46 PM
Registered User
 
Join Date: Feb 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?
Reply With Quote
  #6 (permalink)  
Old March 12th, 2009, 02:11 AM
Registered User
 
Join Date: Mar 2009
Location: Shanghai China
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can declare an object in the static method,
and you have access to private or protected members of the object.
Reply With Quote
  #7 (permalink)  
Old March 12th, 2009, 10:39 AM
Registered User
 
Join Date: Feb 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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;
  }
};
Reply With Quote
  #8 (permalink)  
Old March 12th, 2009, 03:16 PM
Registered User
 
Join Date: Feb 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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();
  }
};
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 02:48 PM
METHODS magagulad Java GUI 3 May 15th, 2007 01:53 PM
Dispose() methods bpevangelista BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 2 May 13th, 2007 12:15 AM
What's the difference between these two methods? aaaa0441 Beginning PHP 2 January 18th, 2007 09:12 AM
Please help me with web methods DiegoF .NET Web Services 3 April 10th, 2004 12:10 PM



All times are GMT -4. The time now is 01:15 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.