Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7540501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:39:34+00:00 2026-05-30T07:39:34+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:39:35+00:00Added an answer on May 30, 2026 at 7:39 am

    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    
     };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three classes: Generic, CFG, and Evaluator. Here's Generic: class Generic: public virtual
Say I have three classes: class X{}; class Y{}; class Both : public X,
There are three Timer classes that I am aware of, System.Threading.Timer , System.Timers.Timer ,
Suppose I have three classes. It is valid to instantiate A, but there are
There are three tables, Students, Classes, Student_Class Is it possible to select students who
This is a study project. I have three database classes A,B,C. There is a
I have three classes; Classes A and B both reference class C . How
If I have three classes class A class B extends A class C extends
First I apologize for a somewhat longer sample code and question... There are three
I have three classes that all have a static function called 'create'. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.