Simple OOP problem I can't figure out

The following code is a toy example of what I can't figure out. I'd like to change a templated member of a templated member with a modifying member function but for some reason the modification doesn't work. Thanks for your help.
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
#include <iostream>

template< typename T >
class Node
{
public:
	Node(){ this->val( T() ); }
	Node( T value ){ this->val( value ); }
	
	void val( T value ){ this->value = value;}
	T val(){ return this->value; }

private:
	T value;
};

int main( int argc, char **argv )
{
	Node< Node<int> > node;
	node.val().val( 10 );
	
	// prints 0 (I'd like it to print 10):
	std::cout << node.val().val() << std::endl;

	node.val( Node<int>( 10 ) );
	// now it prints 10:
	std::cout << node.val().val() << std::endl;
	// I'd like to use the modifier function rather than  
	// initialize a new instance if at all possible.

	return 0;
}
Last edited on
you return by value, so the object gets copied.
I think you want to return by reference

T& val(){ return this->value; }
That does the trick. Thanks!
You're welcome
It is not recommend to put this keyword in a constructor.
Ericool wrote:
It is not recommend to put this keyword in a constructor.
Why? What do you recommend as an alternative? I have never heard this advice before.
Last edited on
In the list initializer you will get a warning . It should not cause trouble in most cases , but based on B.Stroustrup :

Here is something that never works: the {body} of a constructor (or a function called from the constructor) cannot get down to a derived class by calling a virtual member function that is overridden in the derived class. If your goal was to get to the overridden function in the derived class, you won’t get what you want. Note that you won’t get to the override in the derived class independent of how you call the virtual member function: explicitly using the this pointer (e.g., this->method()), implicitly using the this pointer (e.g., method()), or even calling some other function that calls the virtual member function on your this object. The bottom line is this: even if the caller is constructing an object of a derived class, during the constructor of the base class, your object is not yet of that derived class. You have been warned.
There is no code in this thread which uses inheritance, so that quote is not relevant.
I'm glad you mentioned the this keyword, that's something that I've been thinking about. What naming convention do you recommend when I have three of the same variable:
1. Construction parameter,
2. get/set member function, and
3. member variable.

Also, is the this keyword also not recommended in non-constructor member functions. I've been using it in inherited classes. The Node<double>::value more than I'd like to type, if there's a better way.
Last edited on
1. As long as the parameters are too many.
2. Yes but not to all data members if possible. The concept is that an instance execute a task to another class or instance but does not give away all its implementation -> encapsulation.
3. Start data name with m_ should be fine.
Topic archived. No new replies allowed.