Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class and the constructor accepts a parameter. For example,
C++
class Entity
{
private:
    int number_;
public:
    Entity(int number):number_(number)
    {
       std::cout << "Entity object created";
    }
}

// header
class SuperEntity
{
private:
   Entity *entity_;

public:
   SuperEntity(int value);
};

// source
SuperEntity::SuperEntity(int value)
{
    entity_ = new Entity(value);
}


class SuperEntity has a private member Entity. Since in order to instantiate Entity you need to pass in an int to the constructor and cannot be done the declaration file because the int value needed to instantiate Entity is not available yet, is it okay to dynamically allocate Entity in SuperEntity's constructor? Is this a bad practice? Thanks.


What I have tried:

This is how I have doing in project when initialisation parameters are required to instantiate an object.
Posted
Updated 1-Jan-18 6:08am
Comments
Usman Hunjra 31-Dec-17 15:29pm    
I think You want to implement ASSOCIATION .
I'm i Right. ?
Usman Hunjra 31-Dec-17 15:35pm    
Please elaborate your project that what actually you want to do ..
It looks like an assignment.
What is your Project Statement ???
Well according to the information you posted and the code:

In the SuperEntity Class there is a pointer to Entity class So in the Constructor you should write this->entity_.setVal(6); write setters and getters and use them in the constructor instead of using member initializer list.
Please describe the problem in detail regarding your Project ..

Also keep in mind code project is an awesome web for learning by discussion.
No body gives you the exact solution to your Assignment..
Vinokanth Velu 31-Dec-17 21:31pm    
I know about setters and getters. My question is how do you declare objects in the header file if the constructor of the class accepts parameters?
Usman Hunjra 31-Dec-17 15:50pm    
yes its a bad idea. You should use Member Initialize list and invoke the constructor of Class Entity because SuperEntity Class has pointer to object of the Class Entity.
Read about classes and constructors as always slow and steady wins the race..
Your basic concepts are very week, work on it before you for relationships implementation..
Vinokanth Velu 31-Dec-17 23:24pm    
Yes member initialiser list is the answer. Thanks a lot mate. As you said my fundamental is very weak. Trying to improve.

You can use the initialization list. You don't need to create it on the heap.

C++
// source
SuperEntity::SuperEntity(int value):entity_(value)
{
    
}

The declaration of the SuperEntity will become,
C++
// header
class SuperEntity
{
private:
   Entity entity_;

public:
   SuperEntity(int value);
};
 
Share this answer
 
It is okay, but consider a better class design like like making SuperEntity a child class from Entity. Why is it a member? Does it really have to be a member? Saving an int isnt a great deal.

You also need to delete the Entity in the SuperEntity destructor code.

Another solution is:
C++
// transfer of ownership!!!
SuperEntity::SuperEntity(Entity *pointer)
{
  entity_ = pointer;
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900