Default constructor conundrum!

Hello guys!
Is this a troll post? You have 108 posts so I wouldn't expect it, but it sure looks like it.
Hold your horses! @TarikNeaj. I just accidentally closed my entry window
This post's default constructor wasn't very useful, eh?
Here finally is my issue, guys.

I just got stumped when trying to write the (seemingly simple) default constructor for a class template that has a private data member and a pointer data member. Consider the class definition below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<class elemType>
class nodeType                       //structure of the node                                                                                                                                                                             
{
public:
	const nodeType<elemType>& operator=(const nodeType<elemType>&);
        void setInfo(const elemType& elem);
	elemType getInfo() const;
	void setLink(nodeType<elemType>* ptr);
	nodeType<elemType>* getLink() const;
	nodeType();
	nodeType(const elemType& elem, nodeType<elemType>* ptr);
	nodeType(const nodeType<elemType>& otherNode);
	~nodeType();
	
private:
	elemType info;
	nodeType<elemType>* link;
};


I easily wrote the constructor with parameters as shown below:

1
2
3
4
5
6
template<class elemType>
nodeType<elemType>::nodeType(const elemType& elem, nodeType<elemType>* ptr)
{
    info = elem;
    link = ptr;
}


but for the default constructor, I'm somewhat puzzled:

1
2
3
4
5
6
template<class elemType>
nodeType<elemType>::nodeType()
{
    info = ??????????????? //what default literal constant do I assign here?
    link = NULL;
}



The problem I'm having is determining what value/constant to assign the data member 'info'. Since the class is a template, elemType could be an integer, a double, a string, etc., hence, the default value for info might be 0, "", etc.

How do I navigate this problem?
It would probably have saved you a lot of time if you googled "Initialize template variable".

Try this - http://stackoverflow.com/questions/2143022/how-to-correctly-initialize-variable-of-template-type
I wouldn't even supply a default constructor.
> The problem I'm having is determining what value/constant to assign the data member 'info'
> elemType could be an integer, a double, a string, etc.

Make the default a value initialised object. http://en.cppreference.com/w/cpp/language/value_initialization
(This mimics the behaviour of standard sequence containers.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <iomanip>
#include <iterator>

template < typename T > struct node
{
    node() = default ;
    /* explicit */ node( const T& value, node* next = nullptr ) : value(value), next(next) {}
    /* explicit */ node( T&& value, node* next = nullptr ) : value( std::move(value) ), next(next) {}

    T value {} ;
    node* next = nullptr ;
};

int main()
{
    constexpr std::size_t N = 10 ;

    {
        // last four elements are value initialised (zero initialised)
        node<int> nodes[N] { {12}, {15}, {67}, {89}, {32}, {45} } ;
        for( auto p = nodes ; p != nodes+N-1 ; ++p ) p->next = p+1 ;

        for( auto lst = nodes ; lst ; lst = lst->next ) std::cout << lst->value << ' ' ;
        std::cout << '\n' ;
    }

    {
        // last four elements are value initialised (default constructed)
        node<std::string> nodes[N] { {"abc"}, {"de"}, {"fghij"}, {"k"}, {"lmnop"}, {"qrstuvwxyz"} } ;
        for( auto p = nodes ; p != nodes+N-1 ; ++p ) p->next = p+1 ;

        for( auto lst = nodes ; lst ; lst = lst->next ) std::cout << std::quoted(lst->value) << ' ' ;
        std::cout << '\n' ;
    }

    {
        struct A { std::string str ; A( std::string str ) : str(str) {} };

        // node<A> nodes[N] { {{"abc"}}, {{"de"}}, {{"fghij"}}, {{"k"}}, {{"lmnop"}}, {{"qrstuvwxyz"}} } ; 
        // *** error: A is not DefaultConstructible

        node<A> nodes[] { {{"abc"}}, {{"de"}}, {{"fghij"}}, {{"k"}}, {{"lmnop"}}, {{"qrstuvwxyz"}} } ;
        for( auto p = std::begin(nodes) ; p != std::end(nodes)-1 ; ++p ) p->next = p+1 ;

        for( auto lst = std::begin(nodes) ; lst ; lst = lst->next ) std::cout << std::quoted(lst->value.str) << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/e229f7a3c96d9967
Last edited on
Topic archived. No new replies allowed.