Jump to content
  • Advertisement
Sign in to follow this  
danong

[help] c++ pointer

This topic is 3813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for being troublesome, I would like to ask if it is possible to declare a generic pointer to either the base_class or the derived_class at once? I mean, for example, i have a base_class and a derived_class, and i need a pointer that can either pointing to the address of base_class or derived_class, is it possible? Because sometimes in my program, i need to pass to some other functions as class_parameter with base_class and derived_class simultaneously. So what i hope is that, there's a generic pointer that can point to either one of them. Hope to seek for advice, Thanks in advance. :D

Share this post


Link to post
Share on other sites
Advertisement
You need to review polymorphism


Quote:

So what i hope is that, there's a generic pointer that can point to either one of them.


Yes, a base class pointer can point to either a base or derived object.

There is a lot to be said on this topic, can you post some (pseudo) code of what you want to achieve?

Share this post


Link to post
Share on other sites
As already said, there a lot to say about this topic. Give a look to the 'virtual' keyword for an example of what you want.
You can 'cast' a pointer to a base class to a pointer to a derived class (or the way around), adn youo can define in derived classes methods with the same name of base ones wich will be called in place of the base version when that method is called on a derived instance (that is, you can 'override' the default behaviour of that method for a specific derived class).

These are just a couple of tips just to help you with a few key words.

Share this post


Link to post
Share on other sites
ok, i got it solved.
i casted it somehow.

now i'v met a new problem here.
it seems like casting it or either pointing it with a base pointer will have problem in holding the old data, why is it?


i present the snippet of my code here :

class base {
public:

int code; // id

base(){};
base(int id){
code = id;
}
~base() {};

void Update( int num, base * pGroup );

};


class derived : public base {

public:
derived(){};
derived(int id) : base(id) {

};
~derived(){};

derived * group; // pointer to group

};


void base::Update(int num, base * pGroup){
// update
for(int i = 0; i != num; i++){
cout << pGroup.code << ", ";
}
}

int main(){

derived * myDerived;
myDerived = new derived[10];

// update code
for(int i = 0; i != 10; i++){
myDerived = derived(i);
}

// update group
for(i = 0; i != 10; i++){

/* PROBLEM OCCURED HERE */
myDerived.Update( 10, static_cast<base *>(myDerived) );
cout << endl;
}

cout <<" "<<endl;
for(i = 0; i != 10; i++){
cout << myDerived.code << endl;
}

return 0;

}


Hope to seek for advice,
Thanks in advance. :)

Share this post


Link to post
Share on other sites
It would be far easier to help if your classes and methods had more meaningful names.

A few problems I see with your program:
* The base class should always have a virtual destructor.
* The method called "Update" doesn't update anything. The way it is written now, it looks like it should be static, since it doesn't the object on which it's called at all.
* You can't expect pointer arithmetic (`pGroup') to work after casting. The base class and the derived class have different sizes, so array access will need to know the exact types. You can have polymorphic behavior in an array by making it an array of pointers to base.

Share this post


Link to post
Share on other sites
Also, unless a method is declared as 'virtual' in base_class, a derived_class pointed to by a base_class* will use the method as defined by base_class, not derived_class. Just FYI for when you eventually get to overriding that Update method.

Share this post


Link to post
Share on other sites
ya, finally i got it worked.
i've made some studies on these and finally got to understand,
once again, thanks for helping :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!