Property behavior through proxy object

Hello, all!

I just started learnin' myself C++ this week and coming from Python, there are a lot of concepts that are similar, but certainly a lot that are different! One thing I like in Python is the concept of properties where accessor and mutator functions are implicitly called whenever a property is gotten or set, respectively. Looking at operator overloading and the conversion operators, I though I could implement this behavior correctly as a good way to teach myself the syntax and inner workings of the language.

I finally figured out (or I think that I figured out) the syntax so that I no longer get compile-time errors with g++ without an "-std" switch, but I am getting segmentation faults now. I have two source files:

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
// property.hxx:

#ifndef _PROPERTY_H_
#define _PROPERTY_H_


#include <string>


namespace skilly
{
	using std::string;

	template <typename T>
	class property
	{
	public:
		property(T value)
		{
			set(value);
		}

		T operator =(T value)
		{
			set (value);
		}

		operator T() const
		{
			return get();
		}

		T get() const
		{
			return m_value;
		}

		T set(T value)
		{
			m_value = value;
		}

	protected:
		T m_value;
	};
}


#endif


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
//testprop.cxx:

#include <iostream>


#ifndef _PROPERTY_H_
#include "property.hxx"
#endif


namespace skilly
{
	// strings:

	template <>
	string property <string>::get() const
	{
		std::cout << "get string" << std::endl;
		return m_value;
	}

	template <>
	string property <string>::set(string value)
	{
		std::cout << "set string" << std::endl;
		m_value = value;
	}
}


using namespace std;
using namespace skilly;


int main(int argc, char *argv[])
{
	string s = string("Hello, World!");
	skilly::property <string> s2 = skilly::property <string> (s);

	std::cout << (string)s2 << std::endl;

	return 0;
}



When I compile with: g++ -O0 -ggdb -o ./test ./testprop.cxx I get no error messages, but then when I run, I'm getting a "Segmentation fault (core dumped)" message. I'll follow up with the output I get from valgrind, but it looks to me like it has something to do with temporary values; when I don't use s and try to pass the string("Hello, World!") to the property constructor, I get different errors.


Is there anything here that jumps out at anyone? If there's any more information that is needed, please let me know!
One think I wonder about this function.

1
2
3
4
5
6
7
8
9
10
11
12
13
template <>
	string property <string>::get() const
	{
		std::cout << "get string" << std::endl;
		return m_value;
	}

	template <>
	string property <string>::set(string value)
	{
		std::cout << "set string" << std::endl;
		m_value = value;
	}


Just out of curiousity, Whats those template<> doing there? Because I dont think templates work quite that way.And You're not using the template T, so whats that syntax for?

Another thing, About this one

1
2
3
4
5
string property <string>::set(string value)
	{
		std::cout << "set string" << std::endl;
		m_value = value;
	}


is there any reason it is of type string, if it doesnt return a string?
Last edited on
TarikNeaj,

Thanks for the reply!

I am using the "template<>" part there because when I remove it, I am getting the following errors:
1
2
3
4
5
6
./testprop.cxx:14:9: error: specializing member ‘skilly::property<std::basic_string<char> >::get’ requires ‘template<>’ syntax
  string property <string>::get() const
         ^
./testprop.cxx:21:9: error: specializing member ‘skilly::property<std::basic_string<char> >::set’ requires ‘template<>’ syntax
  string property <string>::set(string value)
         ^

When I add the "template<>" part back in, the compiler stops complaining.

is there any reason it is of type string, if it doesnt return a string?

This solves my issue completely! As soon as I included "return m_value" to the end of set(), all my problems disappeared, even when passing string("Hello, World!") directly into the property constructor.

Thank you!
Topic archived. No new replies allowed.