C++ Object Instantiation

Different methods for instantiating an object in C++

The second is wrong !

You may use

MyClass object;

That will work.

Now, concerning how to choose between these two possibilities, it mainly depends on how long your object should live. See there for a thorough answer.

C++ Object Instantiation

On the contrary, you should always prefer stack allocations, to the extent that as a rule of thumb, you should never have new/delete in your user code.

As you say, when the variable is declared on the stack, its destructor is automatically called when it goes out of scope, which is your main tool for tracking resource lifetime and avoiding leaks.

So in general, every time you need to allocate a resource, whether it's memory (by calling new), file handles, sockets or anything else, wrap it in a class where the constructor acquires the resource, and the destructor releases it. Then you can create an object of that type on the stack, and you're guaranteed that your resource gets freed when it goes out of scope. That way you don't have to track your new/delete pairs everywhere to ensure you avoid memory leaks.

The most common name for this idiom is RAII

Also look into smart pointer classes which are used to wrap the resulting pointers on the rare cases when you do have to allocate something with new outside a dedicated RAII object. You instead pass the pointer to a smart pointer, which then tracks its lifetime, for example by reference counting, and calls the destructor when the last reference goes out of scope. The standard library has std::unique_ptr for simple scope-based management, and std::shared_ptr which does reference counting to implement shared ownership.

Many tutorials demonstrate object
instantiation using a snippet such as ...

So what you've discovered is that most tutorials suck. ;)
Most tutorials teach you lousy C++ practices, including calling new/delete to create variables when it's not necessary, and giving you a hard time tracking lifetime of your allocations.

C++ Object Instantiation - Which constructor is called when an object is instantiated using empty parenthesis

All the three declarations are function declarations, but not variable definition (as you expected).

Object b(); declares a function named b, which takes nothing and returns Object.

Object f ( Object() ); declares a function named f, which takes a unnamed parameter whose type is a function returning Object and taking nothing (i.e. Object()), and returns Object. Similarly for Object h ( Object() );.

As the workaround, for Object b(); you can

Object b;
Object b{}; // since C++11

For Object f ( Object() ); you can

Object f ( ( Object() ) );
Object f ( Object{} ); // since C++11
Object f { Object{} }; // since C++11
Object f { Object() }; // since C++11

See most vexing parse for more.

C++ Object Instantiation vs Assignment

TestClass t;

calls the default constructor.

TestClass t = TestClass();

is a copy initialization. It will call the default constructor for TestClass() and then the copy constructor (theoretically, copying is subject to copy elision). No assignment takes place here.

There's also the notion of direct initialization:

TestClass t(TestClass());

If you want to use the assignment operator:

TestClass t;
TestClass s;
t = s;

C++ What is the difference between definition and instantiation?

There are:

1) variable definitions,

2) variable/object instantiations, and

3) template instantiations.

1 & 3 are specific C++ terminology. 2 is more general terminology that might be used with C++. It is not an "officially" defined term for C++.

I understand that your question is about 1 and 2, but not 3. 3 is different than 2, though related in meaning. I won't address 3 further as I don't believe it is part of your question.

Instantiation is the creation of an object instance. It is more usual to use the term in reference to a class object than something like an int or double.

A C++ variable definition does cause an object of the type being defined to be instantiated. It is, however, possible in C++ to instantiate an object other than via a variable definition.

Example 1:

std::string name;

The variable name, a std::string, is defined and (at run-time) instantiated.

Example 2:

std::string *namePointer;

The variable namePointer, a pointer, is defined and might be said (at run-time) to be instantiated (though not initialized). There is no std::string variable and no std::string is instantiated.

//simple example, not what one should usually write in real code
namePointer = new std::string("Some Text");

No additional variable is defined. A std::string object is instantiated (at run-time) and the separate and pre-existing namePointer variable also has its value set.

How do I instantiate an object inside of a C++ class?

class class1
{
//...
};

class class2
{
class1 member;
//...
};

In class2 ctor, you can initialize member in the constructor initialization list.

class2::class2(...)
: member(...)
{
//...
}

C++: instantiating object that is declared in a header file

Object obj;

already instantiates the object. SO you really should not have it in a header file

you probably want

Object *obj;

ie a pointer to an object , then instantiate with 'new'.

But better would be std::unique_ptr<Object>

Object Instantiation in c ++ with a protected constructor

In C++, Static variables/methods are access using scope resolution (::) operator.

change your code to

static MyClass * m_object = MyClass::Create();


Related Topics



Leave a reply



Submit