简体   繁体   中英

Invoking base class constructor in constructor of inherited class

I'm looking at an example of a wrapper class in this book . The wrapper class itself is shown at the end of this post.

On page 94 of the book there is an example of an inherited class. I have a question to what is going on in the initializer list of the constructor:

class RandomBase
{
public:
  RandomBase(unsigned long Dimensionality);
  virtual RandomBase *clone() const = 0;
  virtual ~RandomBase(){};

private:
  unsigned long Dimensionality;
};

class AntiThetic : public RandomBase
{

public:
  AntiThetic(const Wrapper<RandomBase> &innerGenerator) : RandomBase(*innerGenerator),
                                                          InnerGenerator(innerGenerator) {}
  virtual RandomBase *clone() const
  {
    return new AntiThetic(*this);
  }

private:
  Wrapper<RandomBase> InnerGenerator;
};

First of all, I can't compile this. I get the error: error: invalid conversion from 'const RandomBase*' to 'long unsigned int' [-fpermissive] . Secondly, I don't understand conceptually what is going on when we call RandomBase(*innerGenerator) - I didn't know it was possible to do this with C++ (?)


Here is the wrapper class Wrapper:

#ifndef WRAPPER_H
#define WRAPPER_H

template< class T>
class Wrapper
{
public:

    Wrapper()
    { DataPtr =0;}

    Wrapper(const T& inner)
    {
        DataPtr = inner.clone();
    }

    ~Wrapper()
    {
        if (DataPtr !=0)
            delete DataPtr;
    }

    Wrapper(const Wrapper<T>& original)
    {
        if (original.DataPtr !=0)
            DataPtr = original.DataPtr->clone();
        else
            DataPtr=0;
    }

    Wrapper& operator=(const Wrapper<T>& original)
    {
        if (this != &original)
        {
            if (DataPtr!=0)
                delete DataPtr;

            DataPtr = (original.DataPtr !=0) ? original.DataPtr->clone() : 0;
        }

        return *this;
    }


    T& operator*()
    {
        return *DataPtr; 
    }

    const T& operator*() const
    {
        return *DataPtr; 
    }

    const T* const operator->() const
    {
        return DataPtr;
    }

    T* operator->()
    {
        return DataPtr;
    }


private:
    T* DataPtr;


};
#endif

Wrapper taken from here

The error:

invalid conversion from ‘const RandomBase*’ to ‘long unsigned int

is self-explanatory, the constructor:

RandomBase(unsigned long Dimensionality);

expects an unsigned long yet when invoked you pass a *innerGenerator .

Conceptually what is going on is that your child class AntiThetic must invoke the base class RandomBase in its constructor.

So you either need to pass the correct parameter Dimensionality or add an empty constructor.

The wrapper seems to be a template class which allows storage and retrieval of the template parameter T which in this instance would be RandomBase .

What is confusing is why compose both a wrapper of that type and also inherit from it.

You could either inherit from RandomBase (as you already are), inherit from Wrapper<RandomBase> or compose them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM