Solved

About class member pointer

Posted on 2003-11-24
9
304 Views
Last Modified: 2010-04-01
#include<iostream.h>
#include<string>
#include <memory>
using namespace std;
class CMyClass;
typedef void (CMyClass::*TraceMsg)();
class CMyClass  
{
public:
      CMyClass();
      virtual ~CMyClass();

      TraceMsg tracemsg;

      void DefaultTrace( ){
      cout <<"test";
      };

      void Do()
      {
            (this->*tracemsg)();//is ok
      }
};

CMyClass::CMyClass()
{
      this->tracemsg = DefaultTrace;            
}

CMyClass::~CMyClass()
{}
int main()
{  
            CMyClass x;
      x.Do() //is ok;
            (x.*tracemsg)(); //wrong
      return 1;
}
(x.*tracemsg)() doesnt work ,why?
0
Comment
Question by:fyf7262284
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 15

Expert Comment

by:efn
ID: 9816381
tracemsg is a member, so you need to specify in which CMyClass object to find it.  That object is not necessarily x, though it could be.  For example:

(x.*(x.tracemsg))();

Also you need a semicolon at the end of the preceding line.

--efn
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9816399
efn, GCC gives an error regarding:

line 28 # Nonstatic member function "void CMyClass::DefaultTrace()" is used incorrectly (must be either called
    or used to form a pointer to member).
         this->tracemsg = DefaultTrace;

Why does GCC not support this, while VC++ does?
0
 
LVL 15

Expert Comment

by:efn
ID: 9816462
> Why does GCC not support this, while VC++ does?

Because GCC conforms better to the C++ language standard.  Code like that line is not supposed to compile.  It should look like this:

this->tracemsg = &CMyClass::DefaultTrace;

The standard (ISO/IEC 14882:1998) says:

"A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosed in parentheses. [Note: that is, the expression &(qualified-id), where the qualified-id is enclosed in parentheses, does not form an expression of type ?pointer to member.?  Neither does qualified-id, because there is no implicit conversion from a qualified-id for a nonstatic member function to the type ?pointer to member function? as there is from an lvalue of function type to the type ?pointer to function? (4.3). Nor is &unqualified-id a pointer to member, even within the scope of the unqualified-id?s class.]"

(5.3.1, Unary operators, clause 3)

--efn
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:efn
ID: 9816467
Sorry, I missed the quotation marks in my editing.

"A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosed in parentheses. [Note: that is, the expression &(qualified-id), where the qualified-id is enclosed in parentheses, does not form an expression of type 'pointer to member.'  Neither does qualified-id, because there is no implicit conversion from a qualified-id for a nonstatic member function to the type 'pointer to member function' as there is from an lvalue of function type to the type 'pointer to function' (4.3). Nor is &unqualified-id a pointer to member, even within the scope of the unqualified-id's class.]"
0
 
LVL 2

Expert Comment

by:JensUniweb
ID: 9816513
First of all. There is a ; missing. On the line x.Do() there is a ; missing.

The way you're doing it is not allow in the C++ standard. It is forbidden to use pointers to functions in a class. So you need a static function. And that might not do the jobb you want.

What version of gcc did you use. For correct C++ handling you should use 3.0 or later. In 3.3 that I use it gives the correct error that you can not have a pointer to a function in a class.

Borland C++ Builder 6 does not like this eather.

What version of VC++??? Version 6.0 does not follow the C++ standard. It implements its own standard.

BTW: Don't have .h on the standard C++ include files. The correct way is #include <iostream>.
0
 
LVL 2

Expert Comment

by:JensUniweb
ID: 9816521
So, the pointer is allowed. I thought it not. Have to try.
0
 
LVL 1

Author Comment

by:fyf7262284
ID: 9816582
where can i get the "The standard (ISO/IEC 14882:1998) " document?
0
 
LVL 15

Accepted Solution

by:
efn earned 20 total points
ID: 9816630
> where can i get the "The standard (ISO/IEC 14882:1998) " document?

See:

http://www.parashift.com/c++-faq-lite/big-picture.html#faq-6.12

That page tells you about getting it from ISO and ANSI.  It's probably also available through national standards administrations in other countries.

--efn
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10285815
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: efn {http:#9816630}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question