Jump to content

  • Log In with Google      Sign In   
  • Create Account

Making a copy of object from a pointer but dont know its type


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1208

Like
0Likes
Like

Posted 14 April 2014 - 07:00 AM

Hello.

In the main where i make a copy of above objects(bellow that comment) i don't know how to determinate what type the object it is so i wrote (a) for this example

[code]
class a
{
public:
    virtual void Print();
};

class b : public a
{
public:
    void Print();
};

class c : public a
{
public:
    void Print();
};
//////////////////////////////////////////
void a::Print()
{
    std::cout<<"a"<<std::endl;
}
void b::Print()
{
    std::cout<<"b"<<std::endl;
}
void c::Print()
{
    std::cout<<"c"<<std::endl;
}
//////////////////////////////////////////
int main()
{
    std::unique_ptr<a> objA1 = std::unique_ptr<a>(new b);
    std::unique_ptr<a> objA2 = std::unique_ptr<a>(new c);
    //Make a copy of above objects
    std::unique_ptr<a> objA3 = std::unique_ptr<a>(new a/*this a i do not know to to determinate its type*/(*objA1));
    std::unique_ptr<a> objA4 = std::unique_ptr<a>(new a/*also this a i do not know to to determinate*/(*objA2));

    objA1->Print();
    objA2->Print();
    objA3->Print();
    objA4->Print();

    system("pause");
    return 0;
};
[/code]

Output is

b
c
a
a

But i need it to be (b c b c) how can i achieve that?


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 4140

Like
2Likes
Like

Posted 14 April 2014 - 07:18 AM

The general solution is to add a 'virtual a* clone() const' function and require that every subclass implements it correctly.

#3 Juliean   GDNet+   -  Reputation: 2665

Like
3Likes
Like

Posted 14 April 2014 - 09:16 AM

You can even automate the process of defining the clone-methods by adding a intermedia templated class:

class a
{
public:

	virtual a* Clone(void) = 0;
	virtual void Print();
}

template<typename Class>
class a1 : public a
{
public:

	a* Clone(void) const override final
	{
		return new Class(*this);
	}
}

class b : public a1<b>
{
public:

	void Print();
}

class c : public a1<c>
{
public:

	void Print();
}

This can be usefull in reducing redundant code, since many/all implementations of clone will probably just call the classes copy-ctor in the first place, specially if you have many classes that derive from "a".



#4 BaneTrapper   Members   -  Reputation: 1208

Like
0Likes
Like

Posted 14 April 2014 - 05:16 PM

Ive bean reading true changes in c++11 and i think i will change the design not to use polymorphism in this part, thanks on help anywai.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS