-2

Suppose we have an Abstract class and child (derived from abstract) classes. I know we can instantiate from derivedClass like this:

AbstractBase *foo = new DerivedClass1();

But, is this the right way to define the top line code:

AbstractBase foo = *(new DerivedClass1());

I don't want to declare the Abstract class via pointer. But, What is the best way to do this and manage the memeory leak? Thanks a lot

Drewness
  • 5,004
  • 4
  • 32
  • 50
Mosi
  • 1,178
  • 2
  • 12
  • 30
  • Wouldn't it be more clear to write something like `AbstractBase foo; &foo = new DerivedClass1();`? – abiessu Feb 27 '14 at 16:25
  • hmmmmmm, maybe, but my concern is about memory leak?! – Mosi Feb 27 '14 at 16:27
  • when you have pointers you have to deal with memory leaks; if don't want to use pointers the only thing left is to instantiate the derived class directly. The whole point of the abstract class is to be able to swap between derived class intances. – Pandrei Feb 27 '14 at 16:27
  • 1
    By dereferencing it and storing it in foo you slice of the derived part of the class, which causes the leak. You have to store a pointer to the derived class in able to correctly delete it. – Coert Metz Feb 27 '14 at 16:27
  • Perhaps the best question is "why?" since there are obvious problems that can arise. In particular, is there any reason not to use a pointer? – abiessu Feb 27 '14 at 16:30
  • I dont want to write a deep copy! – Mosi Feb 27 '14 at 16:31
  • So now we get to the root of the issue: you don't want to have to write associated code. So instead of writing a deep copy yourself, you want the compiler/runtime to do an object copy when you write `AbstractBase bar = foo;` later on. How do you know that such a mechanism will copy the way you want it to? – abiessu Feb 27 '14 at 16:36
  • Ok, Is there any solutions except writing a deep copy? – Mosi Feb 27 '14 at 16:37
  • Not that I know of. Is this particular deep copy so very painful to write? It seems to me that a copy constructor could be spread out along the class hierarchy so that each particular mechanism does a little bit of work and passes the rest to the next parent class... – abiessu Feb 27 '14 at 16:39
  • You cannot have instances of abstract classes, period! So what is that chitchat about _deep copies_ all about here?? – πάντα ῥεῖ Feb 27 '14 at 17:03
  • 1
    @πάνταῥεῖ: it seems that the OP wants to avoid writing code associated with making copies of objects within a polymorphism scenario, and wishes instead to force the compiler/linker/runtime to do this work; thus the OP chose to attempt to create an instance of an abstract class as a way of making this happen. – abiessu Feb 27 '14 at 17:36
  • @abiessu Even if there wouldn't be an abstract class in this _polymorphism scenario_, the OP would end up with object splicing, no matter of handwritten or compiler generated copy operations. – πάντα ῥεῖ Feb 27 '14 at 18:15
  • @πάνταῥεῖ: very true, thus my comment/answer suggesting to split the copy constructor coding across all the classes in the hierarchy while using the correct mechanism that does not splice the objects... – abiessu Feb 27 '14 at 18:48

2 Answers2

3

AbstractBase foo = *(new DerivedClass1()); will try to construct an instance of AbstractBase (which you can't do, it's abstract) using a constructor which takes an object of type DerivedClass1 (or something it's convertible to.

If you don't want to end up with a pointer to AbstractBase, but you do want to use new to allocate the object dynamically, then you probably want:

AbstractBase& foo = *(new DerivedClass1());

to define a reference to AbstractBase from your new DerivedClass1

Chowlett
  • 45,935
  • 20
  • 116
  • 150
  • I want to construct abstract class from child and not to define the abstract as a pointer. – Mosi Feb 27 '14 at 16:28
  • The reason is to prevent writing a deep-copy! – Mosi Feb 27 '14 at 16:29
  • @Mosi - You can't construct an abstract class - it's abstract. That's what it means. – Chowlett Feb 27 '14 at 16:29
  • @Mosi - what do you mean, prevent writing a deep-copy? – Chowlett Feb 27 '14 at 16:31
  • Suppose I have stored the abstract pointer in an another class(let's call it storeClass) and I want to make a copy of it. How can i do that. I have to write a deep copy from that. – Mosi Feb 27 '14 at 16:32
1

Here is a very simple example of how to "chain" copy constructors. Note that the syntax may be a bit off, but this is the general idea:

class ABase {
 public:
  ABase(const ABase& ab) {
    basevar = ab.getBasevar();
  }
  int getBasevar() { return basevar; }
 private:
  int basevar;
};

class c1 : public ABase {
 public:
  c1(const c1& c) : ABase(c) {
    cvar = c.getCvar();
  }
 private:
  int cvar;
};

In particular, note the use of the : operator between the function declaration and definition; this space allows for multiple direct assignments in a constructor function (possibly in other functions as well) and its content is called an "initialization list". Note also this answer which details some more examples.

Community
  • 1
  • 1
abiessu
  • 1,907
  • 1
  • 18
  • 16
  • Suppose we have a class which take a pointer of the c1 and we have following function: `c1 calculateC1() { c1 sampleC1; // do some stuff with sampleC1 return sampleC1; }` We know that in this function, the return statement will make a copy of c1, So, how the copy contructor works now? – Mosi Feb 27 '14 at 17:03
  • In that case, `return sampleC1;` invokes the copy constructor of the class `c1` as `return c1(sampleC1);` – abiessu Feb 27 '14 at 17:24
  • thanks a lot, But what happens if I write this: `c1 anotherc1Object = calculateC1();` What happens here? – Mosi Feb 27 '14 at 17:36
  • 1
    The same thing; the copy constructor is called once (on return from `calculateC1();` and `anotherc1Object` gets the value. – abiessu Feb 27 '14 at 17:53
  • But all of this should hinge on using the pointer mechanism for objects rather than the direct objects that you are writing; in particular, it should be `c1 *sampleC1 = new c1();` and `c1 *anotherc1Object = calculateC1();` and it should be written `c1 *calculateC1() {...}` to match what will be returned. – abiessu Feb 27 '14 at 17:55