Typecasting a class pointer

Discussion in 'C++' started by Antonio Rivas, Feb 13, 2008.

  1. I've got a code that is the following way:

    classes.h
    class claseA {
    public:
    claseA();
    ~claseA();
    };

    class claseB {
    public:
    claseB();
    claseB( const claseA& aClase);
    ~claseB();
    };

    main.cpp
    #include "classes.h"

    int main () {
    claseA* pClaseA = new claseA();
    claseB* pClaseB = new claseB( pClaseA* ); // compiler error

    .....

    return 0;
    };

    I get a "expected primary-expression before ')'" error at the marked
    line. Seems that try pass a class using a pointer is not valid but when
    I try to typecast the dereferenced pointer this way:

    classB* pClaseB = new claseB( (claseA)pClaseA* );

    I get a different compiler error: "no matching function call to
    'claseA::claseA(claseA*&)'

    In case this compiler error is a precedence error I tried too the
    following code line:

    classB* pClaseB = new claseB( (claseA)(pClaseA*) );

    but I get again the first compiler error: expected primary-expression
    before ')'


    Is clear that I'm missing something, or is just that cannot be done this
    way?
    Antonio Rivas, Feb 13, 2008
    #1
    1. Advertising

  2. Antonio Rivas

    Guest

    On Feb 12, 7:42 pm, Antonio Rivas <> wrote:
    > I've got a code that is the following way:
    >
    > classes.h
    > class claseA {
    >      public:
    >          claseA();
    >          ~claseA();
    >
    > };
    >
    > class claseB {
    >      public:
    >          claseB();
    >          claseB( const claseA& aClase);
    >          ~claseB();
    >
    > };
    >
    > main.cpp
    > #include "classes.h"
    >
    > int main () {
    >     claseA* pClaseA = new claseA();
    >     claseB* pClaseB = new claseB( pClaseA* ); // compiler error
    >
    > ....
    >
    >     return 0;
    >
    > };
    >
    > I get a "expected primary-expression before ')'" error at the marked
    > line. Seems that try pass a class using a pointer is not valid but when
    > I try to typecast the dereferenced pointer this way:
    >
    > classB* pClaseB = new claseB( (claseA)pClaseA* );
    >
    > I get a different compiler error: "no matching function call to
    > 'claseA::claseA(claseA*&)'
    >
    > In case this compiler error is a precedence error I tried too the
    > following code line:
    >
    > classB* pClaseB = new claseB( (claseA)(pClaseA*) );
    >
    > but I get again the first compiler error: expected primary-expression
    > before ')'
    >
    > Is clear that I'm missing something, or is just that cannot be done this
    > way?


    Use *pClaseA instead of pClaseA* as the argument supplied to the
    function call.

    HTH
    , Feb 13, 2008
    #2
    1. Advertising

  3. escribió:
    > On Feb 12, 7:42 pm, Antonio Rivas <> wrote:
    >> I've got a code that is the following way:
    >>
    >> classes.h
    >> class claseA {
    >> public:
    >> claseA();
    >> ~claseA();
    >>
    >> };
    >>
    >> class claseB {
    >> public:
    >> claseB();
    >> claseB( const claseA& aClase);
    >> ~claseB();
    >>
    >> };
    >>
    >> main.cpp
    >> #include "classes.h"
    >>
    >> int main () {
    >> claseA* pClaseA = new claseA();
    >> claseB* pClaseB = new claseB( pClaseA* ); // compiler error
    >>
    >> ....
    >>
    >> return 0;
    >>
    >> };
    >>
    >> I get a "expected primary-expression before ')'" error at the marked
    >> line. Seems that try pass a class using a pointer is not valid but when
    >> I try to typecast the dereferenced pointer this way:
    >>
    >> classB* pClaseB = new claseB( (claseA)pClaseA* );
    >>
    >> I get a different compiler error: "no matching function call to
    >> 'claseA::claseA(claseA*&)'
    >>
    >> In case this compiler error is a precedence error I tried too the
    >> following code line:
    >>
    >> classB* pClaseB = new claseB( (claseA)(pClaseA*) );
    >>
    >> but I get again the first compiler error: expected primary-expression
    >> before ')'
    >>
    >> Is clear that I'm missing something, or is just that cannot be done this
    >> way?

    >
    > Use *pClaseA instead of pClaseA* as the argument supplied to the
    > function call.
    >
    > HTH

    I admit that pointers in C/C++ are quite confusing sometimes. It worked
    like a charm. It's clear that I must study much more the topic about
    pointers :)

    Thank you :)
    Antonio Rivas, Feb 13, 2008
    #3
  4. Antonio Rivas

    Guest

    On Feb 13, 9:24 am, Antonio Rivas <> wrote:
    > escribió:
    >
    > > On Feb 12, 7:42 pm, Antonio Rivas <> wrote:
    > >> I've got a code that is the following way:

    >
    > >> classes.h
    > >> class claseA {
    > >> public:
    > >> claseA();
    > >> ~claseA();

    >
    > >> };

    >
    > >> class claseB {
    > >> public:
    > >> claseB();
    > >> claseB( const claseA& aClase);
    > >> ~claseB();

    >
    > >> };

    >
    > >> main.cpp
    > >> #include "classes.h"

    >
    > >> int main () {
    > >> claseA* pClaseA = new claseA();
    > >> claseB* pClaseB = new claseB( pClaseA* ); // compiler error

    >
    > >> ....

    >
    > >> return 0;

    >
    > >> };

    >
    > >> I get a "expected primary-expression before ')'" error at the marked
    > >> line. Seems that try pass a class using a pointer is not valid but when
    > >> I try to typecast the dereferenced pointer this way:

    >
    > >> classB* pClaseB = new claseB( (claseA)pClaseA* );

    >
    > >> I get a different compiler error: "no matching function call to
    > >> 'claseA::claseA(claseA*&)'

    >
    > >> In case this compiler error is a precedence error I tried too the
    > >> following code line:

    >
    > >> classB* pClaseB = new claseB( (claseA)(pClaseA*) );

    >
    > >> but I get again the first compiler error: expected primary-expression
    > >> before ')'

    >
    > >> Is clear that I'm missing something, or is just that cannot be done this
    > >> way?

    >
    > > Use *pClaseA instead of pClaseA* as the argument supplied to the
    > > function call.

    >
    > > HTH

    >
    > I admit that pointers in C/C++ are quite confusing sometimes. It worked
    > like a charm. It's clear that I must study much more the topic about
    > pointers :)
    >
    > Thank you :)

    hi:
    claseB( const claseA& aClase);
    here means reference, so don't pass a point to it
    , Feb 13, 2008
    #4
  5. escribió:
    > On Feb 13, 9:24 am, Antonio Rivas <> wrote:
    >> escribió:
    >>
    >>> On Feb 12, 7:42 pm, Antonio Rivas <> wrote:
    >>>> I've got a code that is the following way:
    >>>> classes.h
    >>>> class claseA {
    >>>> public:
    >>>> claseA();
    >>>> ~claseA();
    >>>> };
    >>>> class claseB {
    >>>> public:
    >>>> claseB();
    >>>> claseB( const claseA& aClase);
    >>>> ~claseB();
    >>>> };
    >>>> main.cpp
    >>>> #include "classes.h"
    >>>> int main () {
    >>>> claseA* pClaseA = new claseA();
    >>>> claseB* pClaseB = new claseB( pClaseA* ); // compiler error
    >>>> ....
    >>>> return 0;
    >>>> };
    >>>> I get a "expected primary-expression before ')'" error at the marked
    >>>> line. Seems that try pass a class using a pointer is not valid but when
    >>>> I try to typecast the dereferenced pointer this way:
    >>>> classB* pClaseB = new claseB( (claseA)pClaseA* );
    >>>> I get a different compiler error: "no matching function call to
    >>>> 'claseA::claseA(claseA*&)'
    >>>> In case this compiler error is a precedence error I tried too the
    >>>> following code line:
    >>>> classB* pClaseB = new claseB( (claseA)(pClaseA*) );
    >>>> but I get again the first compiler error: expected primary-expression
    >>>> before ')'
    >>>> Is clear that I'm missing something, or is just that cannot be done this
    >>>> way?
    >>> Use *pClaseA instead of pClaseA* as the argument supplied to the
    >>> function call.
    >>> HTH

    >> I admit that pointers in C/C++ are quite confusing sometimes. It worked
    >> like a charm. It's clear that I must study much more the topic about
    >> pointers :)
    >>
    >> Thank you :)

    > hi:
    > claseB( const claseA& aClase);
    > here means reference, so don't pass a point to it

    The problem is that I need pass a pointer since I declare the identifier
    as a member of a class and then I initialize the identifier with new in
    the constructor of this 3rd class. The reason I do it this way is
    because are file I/O classes and to not open/close constantly the file
    in each method I open it in the constructor and I close it in the
    destructor.
    Antonio Rivas, Feb 13, 2008
    #5
    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. venkatesh
    Replies:
    1
    Views:
    8,144
    lallous
    Dec 6, 2003
  2. Robert Street

    Advanced pointer typecasting

    Robert Street, Feb 20, 2004, in forum: C++
    Replies:
    3
    Views:
    6,945
    Robert Street
    Feb 21, 2004
  3. cman
    Replies:
    3
    Views:
    1,617
  4. Arun Prasath
    Replies:
    2
    Views:
    344
    Peter Shaggy Haywood
    Nov 26, 2003
  5. dis
    Replies:
    2
    Views:
    1,465
    Eric Sosman
    Jun 11, 2004
Loading...

Share This Page