6

Is this scenario even possible?

class Base
{
  int someBaseMemer;
};

template<class T>
class Derived : public T
{
  int someNonBaseMemer;

  Derived(T* baseInstance);
};

Goal:

Base* pBase = new Base();
pBase->someBaseMemer = 123; // Some value set
Derived<Base>* pDerived = new Derived<Base>(pBase);

The value of pDerived->someBaseMemer should be equeal to pBase->someBaseMember and similar with other base members.

5
  • 3
    Why do you want such a contorted thing?
    – GManNickG
    Feb 26, 2010 at 21:27
  • StackOverflowException? LOL....On a serious note...this won't compile. Feb 26, 2010 at 21:29
  • @Elite: Actually, it will, if you change the members to be public. Doesn't make it any less terrifying though. Feb 26, 2010 at 21:34
  • It seems to me like you could use this to create an amalgamation of objects that have the functionality you personally want while retaining their own functionality as well. Imagine having a collection that, no matter what, has the ability to call read() and write(), and what is being written or read depends on the device type as well (serial port? file? memory file descriptor?). Seems like a bad design, though. Feb 26, 2010 at 21:36
  • This appears to be a straightforward application of CRTP plus copying from a base class instance, and passing the base object (to copy from) to the Derived ctor as a reference instead of a pointer is more common in C++. en.wikipedia.org/wiki/Curiously_recurring_template_pattern
    – Roger Pate
    Feb 26, 2010 at 22:20

3 Answers 3

4

Why would you want to derive and pass the base pointer at the same time? Choose either, nothing stops you from having both. Use inheritance to do the job for you:

class Base
{
  public:
    Base(int x) : someBaseMemer(x) {}
  protected:      // at least, otherwise, derived can't access this member
    int someBaseMemer;
};

template<class T>
class Derived : public T
{
  int someNonBaseMemer;

  public:
  Derived(int x, int y) : someNonBaseMemer(y), T(x) {}
};

Derived<Base> d(42, 32); // usage

Though not the best of choices as design.

3
  • It surprises me how eager people are to vote up answers the miss the point. What's the point of a Derived(int, int) constructor when the Base class has more members? In your example class Derived : public Base would be better, since the Derived class has to recognize the Base class' members. Yet it still doesn't answer my question.
    – Aoi Karasu
    Feb 26, 2010 at 22:21
  • 1
    You wholly miss my point, unfortunately, which was about letting the compiler do the copying. Your example is a mess -- that doesn't help at all to get your point across.
    – dirkgently
    Feb 26, 2010 at 22:24
  • Luckily there was someone able to understand my mess and answer my question.
    – Aoi Karasu
    Feb 26, 2010 at 22:44
3

Why wouldn't you actually finish writing and compiling the code?

class Base 
{ 
public: // add this
    int someBaseMemer; 
}; 

template<class T> 
class Derived : public T 
{ 
public: // add this
    int someNonBaseMemer; 

    Derived(T* baseInstance)
        : T(*baseInstance) // add this
    { return; } // add this
}; 

This compiles and runs as you specified.

EDIT: Or do you mean that someNonBaseMemer should equal someBaseMemer?

2
  • Derived(T* baseInstance) : T(*baseInstance) is the solution. I tried T(baseInstance) and got stuck on the compiler error. Compiler error messages involving templates eg. SomeClass<T>::NestedClass<T>::blablah give me the creeps. Thank you.
    – Aoi Karasu
    Feb 26, 2010 at 22:07
  • There was another good answer here, but has been deleted. I don't know why. Derived(const T& baseInstance) : T(baseInstance) was also a good hint, but not for my particular case. Example in my question is just a little piece of some bigger stuff.
    – Aoi Karasu
    Feb 26, 2010 at 22:11
1

Declare someBaseMemr as public or change the declaration from class to struct:

class Base
{
  public:
  int someBaseMemer;
};

OR

struct Base
{
  int someBaseMemr;
};

Remember that a class has private access to all members and methods by default. A struct provides public access by default.

Also, all derived classes should have public inheritance from Base.

1
  • The poster's technique is a pattern that allows on to change base classes or the derivation path of the derived class. I've used this technique also. Feb 26, 2010 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.