473,387 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C++ Property template

Hello,

This is my first attempt at template programming so please bear with me.
This is what I have so far:

Property.h --------------------------

#ifndef PROPERTY_H_
#define PROPERTY_H_

enum Access{ READ, WRITE, FULL };

template<typename T, Access type = FULL>
class Property
{
public:
Property(const T& val, T (*getter)(T) = defaultGet, T (*setter)(T,
T) = defaultSet ): value(val) { Get = getter; Set = setter; }
virtual operator T() { return Get(value); }
virtual T operator=( T& val ) { return Set(value, val); }
protected:
T value;
T (*Get) (T);
T (*Set) (T, T);
static T defaultGet(T val) { return val; }
static T defaultSet(T dest, T source) { dest = source; }
};

#endif
and here is my test program:

main.cpp ---------------------------------

#include <cstdlib>
#include <iostream>

#include "Property.h"

static int anotherGetter(int value) { return 10; }

int main(int argc, char *argv[])
{
Property<int> t(1);
std::cout << t << std::endl;
t = 5;
std::cout << t << std::endl;
t = 2;
std::cout << t << std::endl;
// Up to here everything works fine but:
Property<int> f(1, anotherGetter);
std::cout << f << std::endl; // This returns 10 like it's supposed to
f = 5;
std::cout << f << std::endl;
// But this returns 5 not 10
f = 2;
std::cout << f << std::endl;
// And again this returns 2 not 10.
system("pause");
}

My question is why does it work the way I intended the first time but not
the second or third time?

Any help would be appreciated
Nov 23 '05 #1
5 2520
t=5,t=2,f=5,f=2 calls your copy constructor (your constructor is used
as a copy constructor)

Nov 23 '05 #2
On Tue, 22 Nov 2005 06:58:15 -0500, "Brent Ritchie"
<br**********@personainternet.com> wrote:
Hello,

This is my first attempt at template programming so please bear with me.
This is what I have so far:

Property.h --------------------------

#ifndef PROPERTY_H_
#define PROPERTY_H_

enum Access{ READ, WRITE, FULL };

template<typename T, Access type = FULL>
class Property
{
public:
Property(const T& val, T (*getter)(T) = defaultGet, T (*setter)(T,
T) = defaultSet ): value(val) { Get = getter; Set = setter; }
virtual operator T() { return Get(value); }
virtual T operator=( T& val ) { return Set(value, val); }
protected:
T value;
T (*Get) (T);
T (*Set) (T, T);
static T defaultGet(T val) { return val; }
static T defaultSet(T dest, T source) { dest = source; }
};

#endif
and here is my test program:

main.cpp ---------------------------------

#include <cstdlib>
#include <iostream>

#include "Property.h"

static int anotherGetter(int value) { return 10; }

int main(int argc, char *argv[])
{
Property<int> t(1);
std::cout << t << std::endl;
t = 5;
std::cout << t << std::endl;
t = 2;
std::cout << t << std::endl;
// Up to here everything works fine but:
Property<int> f(1, anotherGetter);
std::cout << f << std::endl; // This returns 10 like it's supposed to
f = 5;
std::cout << f << std::endl;
// But this returns 5 not 10
f = 2;
std::cout << f << std::endl;
// And again this returns 2 not 10.
system("pause");
}

My question is why does it work the way I intended the first time but not
the second or third time?

Any help would be appreciated


Your assignment

f = 5;

is inadvertently (to you) interpreted as

f = Property<int>(5);

instead of

f.operator=(5);

In other words, a temporary Property<int> object is created, which is then
assigned to f.

The reason for this behavior is that your assignment operator takes a
non-const T&. Make sure that your assignment operators are in the form:

T& operator=(const T&);

A constructor that can be called with a single argument is also likely to
cause this kind of automatic temporary object creation. Perhaps you should
declare your constructor explicit.
Nov 23 '05 #3
Brent Ritchie wrote:
Property<int> f(1, anotherGetter);
std::cout << f << std::endl; // This returns 10 like it's supposed to
f = 5;
std::cout << f << std::endl;
// But this returns 5 not 10


I guess the problem is this:
The expression 'f = 5' creates a new Property object using the copy
constructor and assigns it to 'f'. Since you are setting the default
getter in the copy constructor, it returns 'value' and not '10', so the
output is '5'.

Try making your copy constructor explicit, then it must not be used for
type conversion. Another issue might be that AFAICT you operator= has a
wrong signature: 'val' should be of type reference-to-const T, not
reference-to-T.

Hope that helps,
Matthias
Nov 23 '05 #4
Matthias Kaeppler wrote:
Try making your copy constructor explicit, then it must not be used for
type conversion.


One more note about copy constructors:

Although your copy constructor has /three/ formal parameters, it can be
called by the client only taking /one/ argument, since you're assigning
default values to parameters two and three.
Since every copy constructor which can be called using one argument
qualifies for implicit type conversion, you should make it 'explicit',
unless you want type conversion to happen (this form of conversion can
be very useful, but it's also error prone because it's hard to
detect--as you probably noted yourself now).

One more C++ subtlety which pops up now and then :)

Regards,
Matthias
Nov 23 '05 #5
Thanks all,

This is what I have so far for my C++ Property Template:

Property.h-------------------------

#ifndef PROPERTY_H_
#define PROPERTY_H_

enum Access{ READ, WRITE, FULL };

template<typename T, Access type = FULL>
class Property
{
public:
explicit Property( const T& val,
const T (*getter)(const T&) = defaultGet,
const T (*setter)(T&, const T&) = defaultSet )
: value(val)
{
if (getter == NULL)
{
Get = defaultGet;
}
else { Get = getter; }
if ( setter == NULL )
{
Set = defaultSet;
}
else
{
Set = setter;
}
}

explicit Property( Property *val )
{
val.value = this->value;
val.Get = this->Get;
val.Set = this->Set;
}

virtual operator T()
{
return Get(value);
}
virtual T operator=( const T& val )
{
return Set(value, val);
}

protected:
const T (*Get) ( const T& );
const T (*Set) ( T&, const T& );
T value;

static const T defaultGet(const T& val)
{
return val;
}

static const T defaultSet(T& dest, const T& source)
{
dest = source;
return dest;
}

};

#endif

main.cpp-----------------------------

#include <cstdlib>
#include <iostream>

#include "../Property.h"

static const int anotherGetter(const int& value) { return value * -1; }
static const int anotherSetter(int& value, const int& value2 ) { value +=
value2; return value; }

int main(int argc, char *argv[])
{
int i = 0;

Property<int> a(2);
std::cout << "Property<int> a(2)" << std::endl;
std::cout << "Input value to test default set functionality: ";
std::cin >> i;
a = i;
std::cout << "Testing default get functionality: ";
std::cout << a << std::endl << std::endl;

Property<int> b(2, anotherGetter);
std::cout << "Property<int> b(2, anotherGetter)" << std::endl;
std::cout << "Input value to test default set functionality: ";
std::cin >> i;
b = i;
std::cout << "Testing anotherGetter functionality: ";
std::cout << b << std::endl << std::endl;

Property<int> c(2, NULL, anotherSetter);
std::cout << "Property<int> c(2, NULL, anotherSetter)" << std::endl;
std::cout << "Input value to test anotherSetter functionality: ";
std::cin >> i;
c = i;
std::cout << "Testing default get functionality: ";
std::cout << c << std::endl << std::endl;

Property<int> d(2, anotherGetter, anotherSetter);
std::cout << "Property<int> d(2, anotherGetter, anotherSetter)" <<
std::endl;
std::cout << "Input value to test anotherSetter functionality: ";
std::cin >> i;
d = i;
std::cout << "Testing anotherGetter functionality: ";
std::cout << d << std::endl << std::endl;

Property<int> x = d;
std::cout << x << std::endl;
x = 2;
std::cout << x << std::endl;
x = 1;
std::cout << x << std::endl;
system("pause");

return 0;

}

Next, I'm going to add the overloaded versions of this template for
read-only and write-only access. Then I'm going to start testing all
primitives not just int. Then I have to start testing user defined classes.
<shutters>

Any comments on what I have so far? Anything you see as potentially
broken? Feature changes/requests ;) ? Anyone think this might be potentially
useful to anyone but me?

Also if you notice this compiles nicely with no warnings even with -Wall
turned on. I think thats a first for me!

Anyways, Thanks for all the help. This template programming isn't all
that different from regular programming but it has it's nuances though.
Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
9
by: Rakesh | last post by:
Hi, I am able to obtain a MenuItem object's Name property @ design-time, but am not able to get the same @ run- time...why? And since MenuItem doesn't inherit from Control class, it's not...
1
by: The_Rave | last post by:
Hi everyone, I'm trying to add my own template columns to the property builder of ..NET. E.g. a checkbox column, or an image column. But I can't find the sources of the wizard, or a way to add...
3
by: Ricardo Maciel | last post by:
The How to page: http://msdn2.microsoft.com/en-us/library/7aw8zc76(VS.80).aspx walks through the creation of MDI child forms. It includes instructions to set the property MdiList to maintain a...
0
by: Memfis | last post by:
While I was looking through the group's archives I came across a discussion on doing properties (as known from Delphi/C#/etc) in C++. It inspired me to do some experimenting. Here's what I came up...
2
by: kashif | last post by:
Hello All, I am trying to crete alerts using SharePoint V3 API and so far I am able to create alerts but for some reason I could not figure out how to set AlertTemplate property on SPAlert object....
1
by: berny.zamora | last post by:
Hello everyone, I have a composite control (lets call it the parent) that contains a datalist. The datalist has an ItemTemplate that contains another composite control (lets call it the child)....
2
by: C. Herzog | last post by:
Hi! I want to create a huge amount of classes. All classes have in common that the change of a property should fire an Change-Event and maybe do other things. I don't want to write thousand...
0
by: christian2.schmidt | last post by:
Hi, I tried to implement the std::swap method on properties (and indexers) without success. The problem is that list calls the getter and returns a copy and the assignment in the swap method...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.