1

There are three classes.The first is a template, the second acts as generic for template and third implements the template.

template <class T> class Shape { T val,val_new; public: Shape(T initval) { val=initval; } ... }; class TwoPoint { int width; int value; public: TwoPoint() { value=0; width=0; } TwoPoint(int v, int w) { value=v; width=w; } TwoPoint(const TwoPoint& t) { value= t.value; width= t.width; } ... }; class Rectangle { private: Shape<TwoPoint> val; TwoPoint newval; public: Rectangle(TwoPoint i) : val (Shape<TwoPoint> (i)) {} .... }; 

I want to initialize the Rectangle and solidShape in some other class as class members and that can be done in java like:

 Rectangle r = new Rectangle(new TwoPoint(0,8)); Shape<TwoPoint> solidShape = new Shape<TwoPoint>(new TwoPoint(0,5)); 

How can i do a similar type of thing in C++? I want to create an implementation like:

 class C { public: // initialize Rectangle here; // initialize solidShape here; } 

The integer values shown here are just for illustration and can be anything.

2 Answers 2

2

The correct way to have a conversion constructor in C++ is through a const reference:

 Rectangle(const TwoPoint& i) 

This also means you can pass a temporary as parameter:

 Rectangle* r = new Rectangle( TwoPoint(0,8) ); //dynamic storage 

or

 Rectangle r( TwoPoint(0,8) ); //automatic storage 

It would also work with a pass by value, but this is the standard way of doing it.

Same goes for the Shape class:

 Shape(const T& initval) //conversion constructor 

and:

 Shape<TwoPoint>* solidShape = new Shape<TwoPoint>( TwoPoint(0,5) ); //dynamic storage 

or

 Shape<TwoPoint> solidShape( TwoPoint(0,5) ); //automatic storage 

In C++, new returns a pointer. But your conversion constructors take objects (not pointers to objects) by reference or value. So you need an object passed as parameter, not pointers.

If these two are class members:

  • if you chose to have pointers, you need to free the memory in the destructor.

  • if you chose automatic storage objects (what you have now), the destructors will be called when the containing object is destroyed, so you don't manually free the memory. To initialize automatic storage objects that are class members, you need to use an initialization list:

Like this:

class C { Shape<TwoPoint> solidShape; Rectangle r; public: C() : solidShape(TwoPoint(0,5)), r( TwoPoint(0,8) ) {} //initialization list in constructor }; 
Sign up to request clarification or add additional context in comments.

12 Comments

Rectangle r = new Rectangle( TwoPoint(0,8) ); is not working
New returns a pointer as he just said - he forgot to make those pointer types,. Should be Rectangle* ...or just non pointer Rectangle r(params);
@Luchian Grigore new Rectangle ... create a pointer and is not possible to assign to a Rectangle objetc. Besides, it's not a temporary variable, is heap allocated and memory leaked.
@TioPepe I had already edited the answer. It was a copy/paste error.
@LuchianGrigore Sorry, I didn't saw it. I'm retracted ;-)
|
0

We use const references as constructor parameters and initialization constructor chain:

template <class T> class Shape { T val,val_new; public: Shape(const T & initval) : val(initval) { } ... }; class TwoPoint { int width; int value; public: TwoPoint() : value(0), width(0) { } TwoPoint(int v, int w) : value(v), width(v) { } TwoPoint(const TwoPoint& t) value(t.value), width(t.width) { } ... }; class Rectangle { private: Shape<TwoPoint> val; TwoPoint newval; public: Rectangle(const TwoPoint & i) : val (Shape<TwoPoint> (i)) {} .... }; 

and you create object like this:

TwoPoint t(0,8) Rectangle r(t); Shape<TwoPoint> shape(t); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.