Constructor

Started by
16 comments, last by Perost 15 years, 4 months ago
Hummm
Why the declarations are pointers to pointers?
Is because when I use new the variable is already a pointer??
Advertisement
I don't get the question. I concluded from your original code that pointers-to-pointers is what you are using, otherwise your code wouldn't compile. Of course you only need a pointer (not pointer-to-pointer) to store the pointer returned by new (unless what you want is an array of pointers). But if you have a pointer-to-pointer then you just can't dereference it (*hp) without first making hp point at something.
Are these declarations illegal??:
Jogador Daniel(23,56,877,132,34,67)=new Jogador;

Jogador Daniel =new Jogador(56,45,67,78,90);

I tried both but compiler says that "thre's no apropiatte constructor",or "'int' no variable before 'new'".

I've token off the allocation in the constructor and I just used "new " to objects.

Ahh and if I put arguments in the constructor appears an error no matters if I use list or just put parameters.Some thing "sutil" seems to existe in the code.
The first is completely wrong. The second is almost correct, except the result of calling new is a pointer and must therefore be stored in a pointer. And naturally Jogador must have a constructor that takes 5 int parameters.

Jogador* Daniel = new Jogador(56,45,67,78,90);
Quote:Original post by visitor
The first is completely wrong. The second is almost correct, except the result of calling new is a pointer and must therefore be stored in a pointer. And naturally Jogador must have a constructor that takes 5 int parameters.

Jogador* Daniel = new Jogador(56,45,67,78,90);
Or alternatively
Jogador Daniel = Jogador(56,45,67,78,90);


And what woulb be the "apropriate" constructor for the class?
Quote:Original post by Asafe
And what woulb be the "apropriate" constructor for the class?



You have to have a constructor whos parameter list matches what you're calling. It's the same as function overloading.


Put it like this, if I have a function that's called like this...

MyFunc(3,4,5,6,7);

What would you expect the prototype to look like?
Quote:Original post by ville-v
Or alternatively
Jogador Daniel = Jogador(56,45,67,78,90);

Or simply
Jogador Daniel(56, 45, 67, 78, 90);


This topic is closed to new replies.

Advertisement