On Mar 5, 11:21 am, "Alf P. Steinbach" <al...@start.no> wrote:
* pauldepst...@att.net:
Class A has two constructors A( someclass& asomeclassvar,
someotherclass& asomeotherclassvar) and also A(someclass&
asomeclassvar, someotherclass& asomeotherclassvar, yetanotherclass&
ayetanotherclassvar) Get and Set methods are available for the
parameters from someclass, someotherclass and yetanother class.
Sometimes these Get and Set Methods are accessed from a base
class
I want to write code like:
A* NewPointerToClass = new(PreviouslyDefinedPointerToMyClass ->
Getasomeclassvar(), PreviouslyDefinedPointerToMyClass-
Getsomeotherclassvar ..);
My problem is that I don't know which of the two possible constructors
is available via PreviouslyDefinedPointerToMyClass
I only want to call a Get method if it corresponds to a variable
that's present in the constructor.
So it seems like I need to know how to say, in c++
if(the thing being pointed to has been defined using such and such a
constructor) do...
else if the thing being pointed to has been defined using such and
such a constructor) do...
I would guess this is a common situation in c++ What is the most
standard way of handling this?
First, the terminology above is quite confused, so I'll have to make a
stab at what you probably mean. My interpretation is: you have a class
A with two constructors; depending on which constructor was used an
instance of A will contain different information; you have a pointer to
such an instance, and you want to clone that instance.
Then, if that interpretation is correct, just define a clone() member
function in class A.
That clone function will itself be very simple:
A* clone() { return new A( *this ); }
using class A's copy constructor.
Then you can write
A* newPointerToClass = previouslyDefinedPointerToMyClass->clone()=
Thanks a lot for your help, Alf. I think I'm going with a different
solution, but your post was very educational to me.
Sorry for the lack of clarity. I didn't know about cloning so I
lacked the vocab to be clearer.