Typecasting a class pointer

A

Antonio Rivas

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?
 
A

AnonMail2005

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
 
A

Antonio Rivas

(e-mail address removed) escribió:
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 :)
 
C

czp.opensource

(e-mail address removed) escribió:




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
 
A

Antonio Rivas

(e-mail address removed) escribió:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,048
Members
48,769
Latest member
Clifft

Latest Threads

Top