a problem about the pointer to class member

Discussion in 'C++' started by miaohua1982@gmail.com, Jan 5, 2007.

  1. Guest

    the code is as follows:

    #include<iostream>
    using namespace std;

    class A
    {
    public:
    int a;
    int b;
    };

    int main(int argc,char **argv)
    {
    int (A::*pInt) = &A::a;
    if ( &A::a )
    {
    cout<<"OK1n"<<endl;
    }
    if( pInt)
    {
    cout<<"OK2"<<endl;
    }
    return 0;
    }

    I run the code in VC7 , VC8
    the problem is why there is only out put "OK2"?
     
    , Jan 5, 2007
    #1
    1. Advertising

  2. Ondra Holub Guest

    napsal:
    > the code is as follows:
    >
    > #include<iostream>
    > using namespace std;
    >
    > class A
    > {
    > public:
    > int a;
    > int b;
    > };
    >
    > int main(int argc,char **argv)
    > {
    > int (A::*pInt) = &A::a;
    > if ( &A::a )
    > {
    > cout<<"OK1n"<<endl;
    > }
    > if( pInt)
    > {
    > cout<<"OK2"<<endl;
    > }
    > return 0;
    > }
    >
    > I run the code in VC7 , VC8
    > the problem is why there is only out put "OK2"?


    I do not know why (maybe bug?). In g++ it works. You can change it to
    if ( &A::a != 0) and it should work.
     
    Ondra Holub, Jan 5, 2007
    #2
    1. Advertising

  3. Guest

    yes if ( &A::a != 0) does work, so another bug in VC8?
    "Ondra Holub дµÀ£º
    "
    > napsal:
    > > the code is as follows:
    > >
    > > #include<iostream>
    > > using namespace std;
    > >
    > > class A
    > > {
    > > public:
    > > int a;
    > > int b;
    > > };
    > >
    > > int main(int argc,char **argv)
    > > {
    > > int (A::*pInt) = &A::a;
    > > if ( &A::a )
    > > {
    > > cout<<"OK1n"<<endl;
    > > }
    > > if( pInt)
    > > {
    > > cout<<"OK2"<<endl;
    > > }
    > > return 0;
    > > }
    > >
    > > I run the code in VC7 , VC8
    > > the problem is why there is only out put "OK2"?

    >
    > I do not know why (maybe bug?). In g++ it works. You can change it to
    > if ( &A::a != 0) and it should work.
     
    , Jan 5, 2007
    #3
  4. Jim Langston Guest

    <> wrote in message
    news:...
    > the code is as follows:
    >
    > #include<iostream>
    > using namespace std;
    >
    > class A
    > {
    > public:
    > int a;
    > int b;
    > };
    >
    > int main(int argc,char **argv)
    > {
    > int (A::*pInt) = &A::a;
    > if ( &A::a )
    > {
    > cout<<"OK1n"<<endl;
    > }
    > if( pInt)
    > {
    > cout<<"OK2"<<endl;
    > }
    > return 0;
    > }
    >
    > I run the code in VC7 , VC8
    > the problem is why there is only out put "OK2"?


    if ( &A::a )
    {
    std::cout << "OK1n" << std::endl;
    }

    warning C4127: conditional expression is constant

    if ( &(A::a) )
    {
    std::cout << "OK1n" << std::endl;
    }
    error C2597: illegal reference to non-static member 'A::a'

    That should tell you what's going on right there.

    a is a non static variable of the class. You are trying to take the address
    of something that doesn't exist. a won't have an address until A is
    instantized.

    A Foo;
    if ( &Foo.a )
    {
    std::cout << "OK3" << std::endl;
    }

    Compiles fine.

    What are you trying to accomplish by taking the address of a non-instantized
    class definition?
     
    Jim Langston, Jan 5, 2007
    #4
  5. Ondra Holub Guest

    napsal:
    > yes if ( &A::a != 0) does work, so another bug in VC8?
    > "Ondra Holub 写é“:


    I would say yes, it is probably bug. I tried it in Borland®
    C++Builder® for Microsoft® Windows™ Version 10.0.2288.42451 too and
    it works without problems.
     
    Ondra Holub, Jan 5, 2007
    #5
  6. Ondra Holub Guest

    Jim Langston napsal:
    > <> wrote in message
    > news:...
    > > the code is as follows:
    > >
    > > #include<iostream>
    > > using namespace std;
    > >
    > > class A
    > > {
    > > public:
    > > int a;
    > > int b;
    > > };
    > >
    > > int main(int argc,char **argv)
    > > {
    > > int (A::*pInt) = &A::a;
    > > if ( &A::a )
    > > {
    > > cout<<"OK1n"<<endl;
    > > }
    > > if( pInt)
    > > {
    > > cout<<"OK2"<<endl;
    > > }
    > > return 0;
    > > }
    > >
    > > I run the code in VC7 , VC8
    > > the problem is why there is only out put "OK2"?

    >
    > if ( &A::a )
    > {
    > std::cout << "OK1n" << std::endl;
    > }
    >
    > warning C4127: conditional expression is constant
    >
    > if ( &(A::a) )
    > {
    > std::cout << "OK1n" << std::endl;
    > }
    > error C2597: illegal reference to non-static member 'A::a'
    >
    > That should tell you what's going on right there.
    >
    > a is a non static variable of the class. You are trying to take the address
    > of something that doesn't exist. a won't have an address until A is
    > instantized.
    >
    > A Foo;
    > if ( &Foo.a )
    > {
    > std::cout << "OK3" << std::endl;
    > }
    >
    > Compiles fine.
    >
    > What are you trying to accomplish by taking the address of a non-instantized
    > class definition?


    It may be used together with some instance (although I never used
    anything like that):

    #include <iostream>

    class A
    {
    public:
    int a;
    int b;
    };

    int main()
    {
    int (A::*pInt) = &A::a;
    A a1;
    A a2;

    a1.*pInt = 5;
    a2.*pInt = 12;
    std::cout << a1.a << ' ' << a2.a << '\n';
    }
     
    Ondra Holub, Jan 5, 2007
    #6
  7. Guest

    the expression of &A::a does not mean to get the address of member a,
    but to get its offset in class A,

    By section 5.3.1/2 of the C++ standard:

    <quote>
    The result of the unary & operator is a pointer to its operand...If the

    member is a nonstatic member of class C of type T, the type of the
    result is
    "pointer to member of class C of type T."
    [Example:
    struct A { int i; };
    struct B : A { };
    .... &B::i ... // has type int A::*
    -end example]
    </quote>



    "Jim Langston дµÀ£º
    "
    > <> wrote in message
    > news:...
    > > the code is as follows:
    > >
    > > #include<iostream>
    > > using namespace std;
    > >
    > > class A
    > > {
    > > public:
    > > int a;
    > > int b;
    > > };
    > >
    > > int main(int argc,char **argv)
    > > {
    > > int (A::*pInt) = &A::a;
    > > if ( &A::a )
    > > {
    > > cout<<"OK1n"<<endl;
    > > }
    > > if( pInt)
    > > {
    > > cout<<"OK2"<<endl;
    > > }
    > > return 0;
    > > }
    > >
    > > I run the code in VC7 , VC8
    > > the problem is why there is only out put "OK2"?

    >
    > if ( &A::a )
    > {
    > std::cout << "OK1n" << std::endl;
    > }
    >
    > warning C4127: conditional expression is constant
    >
    > if ( &(A::a) )
    > {
    > std::cout << "OK1n" << std::endl;
    > }
    > error C2597: illegal reference to non-static member 'A::a'
    >
    > That should tell you what's going on right there.
    >
    > a is a non static variable of the class. You are trying to take the address
    > of something that doesn't exist. a won't have an address until A is
    > instantized.
    >
    > A Foo;
    > if ( &Foo.a )
    > {
    > std::cout << "OK3" << std::endl;
    > }
    >
    > Compiles fine.
    >
    > What are you trying to accomplish by taking the address of a non-instantized
    > class definition?
     
    , Jan 6, 2007
    #7
  8. Guest

    I don't think so , how about the following code?
    #include<iostream>
    using namespace std;

    class A
    {
    public:
    int a;
    int b;
    };

    int main(int argc,char **argv)
    {
    if (&A::a )
    {
    cout<<"OK\n";
    }
    cout<<&A::a<<endl;
    return 0;
    }

    the output is 1 in vc7, which means in "cout" , it does the correct
    check, but why not the "if" expression

    "Ondra Holub дµÀ£º
    "
    > Jim Langston napsal:
    > > <> wrote in message
    > > news:...
    > > > the code is as follows:
    > > >
    > > > #include<iostream>
    > > > using namespace std;
    > > >
    > > > class A
    > > > {
    > > > public:
    > > > int a;
    > > > int b;
    > > > };
    > > >
    > > > int main(int argc,char **argv)
    > > > {
    > > > int (A::*pInt) = &A::a;
    > > > if ( &A::a )
    > > > {
    > > > cout<<"OK1n"<<endl;
    > > > }
    > > > if( pInt)
    > > > {
    > > > cout<<"OK2"<<endl;
    > > > }
    > > > return 0;
    > > > }
    > > >
    > > > I run the code in VC7 , VC8
    > > > the problem is why there is only out put "OK2"?

    > >
    > > if ( &A::a )
    > > {
    > > std::cout << "OK1n" << std::endl;
    > > }
    > >
    > > warning C4127: conditional expression is constant
    > >
    > > if ( &(A::a) )
    > > {
    > > std::cout << "OK1n" << std::endl;
    > > }
    > > error C2597: illegal reference to non-static member 'A::a'
    > >
    > > That should tell you what's going on right there.
    > >
    > > a is a non static variable of the class. You are trying to take the address
    > > of something that doesn't exist. a won't have an address until A is
    > > instantized.
    > >
    > > A Foo;
    > > if ( &Foo.a )
    > > {
    > > std::cout << "OK3" << std::endl;
    > > }
    > >
    > > Compiles fine.
    > >
    > > What are you trying to accomplish by taking the address of a non-instantized
    > > class definition?

    >
    > It may be used together with some instance (although I never used
    > anything like that):
    >
    > #include <iostream>
    >
    > class A
    > {
    > public:
    > int a;
    > int b;
    > };
    >
    > int main()
    > {
    > int (A::*pInt) = &A::a;
    > A a1;
    > A a2;
    >
    > a1.*pInt = 5;
    > a2.*pInt = 12;
    > std::cout << a1.a << ' ' << a2.a << '\n';
    > }
     
    , Jan 6, 2007
    #8
  9. Grizlyk Guest

    Jim Langston wrote:

    > > <> wrote in message
    > > class A
    > > {
    > > public:
    > > int a;
    > > int b;
    > > };
    > >
    > > int (A::*pInt) = &A::a;
    > > if ( &A::a ){ cout<<"OK1n"<<endl; }


    > warning C4127: conditional expression is constant


    Condition ( &A::a ) is always false or true due to class A declaration.
    I supposed, "A::a" has offset 0, so condition ( &A::a ) is always
    false, but i was wrong.

    if ( &A::a ){ cout<<"OK1n"<<endl; }

    compiled to

    movl $0, %eax //&A::a
    cmpl $-1, %eax //if(&A::a)
    je //else

    > a is a non static variable of the class. You are trying to take the address
    > of something that doesn't exist. a won't have an address until A is
    > instantized.
    >
    > A Foo;
    > if ( &Foo.a )


    "&Foo.a" is not the same to "&A::a". &A::a always exist if class A was
    defined.
    Take it
    {
    union
    {
    int (A::*pInt);
    int tmp;
    } x;

    A y;

    cout<< "&y.a= "<< &y.a<< endl;

    x.pInt = &A::a;
    cout<< "&A::a"<< "tmp= "<< x.tmp<< endl;
    cout<< "&A::a"<< "pInt= "<< x.pInt<< endl;

    if ( &A::a ) { cout<<"OK1"<<endl; }
    if ( x.pInt ) { cout<<"OK2"<<endl; }
    cout<< endl;

    cout<< "&y.b= "<< &y.b<< endl;

    x.pInt = &A::b;
    cout<< "&A::b"<< "tmp= "<< x.tmp<< endl;
    cout<< "&A::b"<< "pInt= "<< x.pInt<< endl;

    if ( &A::a ) { cout<<"OK3"<<endl; }
    if ( x.pInt ) { cout<<"OK4"<<endl; }
    cout<< endl;

    x.tmp = -1;
    cout<< "set -1 "<< "tmp= "<< x.tmp<< endl;
    cout<< "set -1 "<< "pInt= "<< x.pInt<< endl;

    if ( x.pInt ) { cout<<"OK5"<<endl; }
    cout<< endl;
    }
     
    Grizlyk, Jan 12, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,941
    Thomas Weidenfeller
    Oct 12, 2005
  2. Fraser Ross
    Replies:
    4
    Views:
    1,094
    Fraser Ross
    Aug 14, 2004
  3. Stephen Howe
    Replies:
    2
    Views:
    306
    Stephen Howe
    Nov 6, 2012
  4. somenath
    Replies:
    10
    Views:
    310
    James Kanze
    Jul 2, 2013
  5. somenath
    Replies:
    2
    Views:
    178
    somenath
    Aug 29, 2013
Loading...

Share This Page