fyf7262284
asked on
About class member pointer
#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?
#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?
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?
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?
> 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
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
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.]"
"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.]"
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>.
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>.
So, the pointer is allowed. I thought it not. Have to try.
ASKER
where can i get the "The standard (ISO/IEC 14882:1998) " document?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
(x.*(x.tracemsg))();
Also you need a semicolon at the end of the preceding line.
--efn