OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to Ensure proper initialization of Non Static Data Members within a Class Template in C++

  • Thread starter Thread starter user16783784
  • Start date Start date
U

user16783784

Guest
I am working with templates in C++ and want to know how can we properly(value) initialize the non static data members in a class template. For example, consider the following snippet:

Code:
template<typename T>
class MyVector
{
    T x; // x has undefined value for a built in type
};

Now i know that the data member x has garbage value for built in types in local/block scope unless explicitly initialized.

So i want to value initialize the data member. If i modify the above code to:

Code:
template<typename T>
class MyVector
{
    T x(); // now x becomes a member function 
};

As can be seen in the above modified code snippet, x is now a member function. How can i value initialize the data member x for type T?
Continue reading...
 

Latest posts

A
Replies
0
Views
1
Ashrik Ahamed
A
A
Replies
0
Views
1
Ashrik Ahamed
A

Online statistics

Members online
1
Guests online
4
Total visitors
5
Top