Sign in to follow this  

grabbing an int form an overloaded function

This topic is 4686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

class Player { private: int _strength; } class Player { void Player::Strength( int strength ) { _strength = strength; } int Player::Strength( void ) { return _strength; } } if I say: TCHAR cString[64]; sprintf( cString, "%d", _player->Strength); I get an error that it cannot determine the overloaded function? How can I get around this. Nick

Share this post


Link to post
Share on other sites
Quote:
Original post by nick5454
if Im using a class how can I make to like a property like ATL? without making it a field.

Nick


If you want to do it like that, then you'd need this:

class Player
{
private:
//int _strength;
public:
int Strength;
};


Also note how the class is setup, I do not think your first post will compile. If you do it like that, then the proper use of a class is broken. You really need to use functions to operate on it, but maybe someone else (looks at Fruny) could propose a better way to accompish this in C++.

- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by nick5454
if Im using a class how can I make to like a property like ATL? without making it a field.


ISO C++ does not support properties. Either make it a public member variable, or access it through a member function. If you want to make it "like ATL", well... look at ATL's source code and see what it does... [smile]

Now, there are ways to make something that is "property-like", but I don't think you'll like it...


#include <iostream>

class Player
{
class Strength
{
int m_value;
public:
explicit Strength(int value) :
m_value(value)
{}

Strength(const Strength& value) :
m_value(value)
{}

private:
int get() const
{
std::cout << "get strength" << std::endl;
return m_value;
}

Strength& set(int value)
{
std::cout << "set strength" << std::endl;
m_value = value;
return *this;
}
public:
operator int() const
{
return get();
}

Strength& operator=(int value)
{
return set(value);
}

Strength& operator=(const Strength& value)
{
return set(value);
}
};
public:
Strength strength;

public:
Player(int str) :
strength(str)
{}
};

int main()
{
Player p(5);
p.strength = 25;

Player q(10);
q.strength = p.strength;

std::cout << p.strength << std::endl;
}







You will need a separate class for each attribute for which you want to create different get/set member functions. Also note that taking its address won't return an int* (hey, it's a property, not an int, after all ... and overloading the unary & operator is widely seen as a bad idea).

If those member functions are just plain 'pass through'... Then you might as well just use a public member variable instead of a "property"... and don't give me any of that "but if I want to change it later" crap.

Share this post


Link to post
Share on other sites
Or, for template lovers.


#include <iostream>

template<class T>
struct PassThroughProperty
{
typedef T value_type;
typedef const T& reference;
private:
T m_value;
public:
explicit PassThroughProperty(reference value) :
m_value(value)
{}

reference get() const
{
return m_value;
}

void set(reference value)
{
m_value = value;
}
};

template<class T>
struct EchoProperty
{
typedef T value_type;
typedef const T& reference;
private:
T m_value;
public:
explicit EchoProperty(reference value) :
m_value(value)
{}

reference get() const
{
std::cout << "Get " << (void*)this << std::endl;
return m_value;
}

void set(reference value)
{
std::cout << "Set " << (void*)this << std::endl;
m_value = value;
}
};

template<class T,
template<typename> class P = PassThroughProperty,
size_t Tag = 0>
class Property
{
typedef P<T> property_type;
typedef typename property_type::value_type value_type;
typedef typename property_type::reference reference;

property_type m_value;
public:
explicit Property(reference value) :
m_value(value)
{}

Property(const Property& value) :
m_value(value)
{}
public:
operator reference() const
{
return m_value.get();
}

Property& operator=(reference value)
{
m_value.set(value); return *this;
}

Property& operator=(const Property& value)
{
m_value.set(value); return *this;
}
private:
// DO NOT IMPLEMENT
template<class T2, template<typename> class P2, size_t Tag2>
Property& operator=(const Property<T2,P2,Tag2>&);
template<class T2, template<typename> class P2, size_t Tag2>
Property(const Property<T2,P2,Tag2>&);

};


class Player
{
public:
Property<int, EchoProperty> strength;

public:
Player(int str) :
strength(str)
{}
};

int main()
{
Player p(5);
p.strength = 25;

Player q(10);
q.strength = p.strength;

std::cout << p.strength << std::endl;
}




The Tag template parameter can be used to prevent (direct) assignment between properties with a different tag value, eg.


typedef Property<int, PassThroughProperty, 0> strength_t
typedef Property<int, PassThroughProperty, 1> dexterity_t;

strength_t str1(25);
strength_t str2(50);

dexterity_t dex(20);

str1 = str2; // OK
str1 = dex; // Does not compile



The code could be modified to only prevent assignment between properties with a different tag, but that's not what the current code does (type, policy and tag must all match to allow direct assignment).

"Indirect" assignment means casting to the element type first: str1 = (int)dex;

Share this post


Link to post
Share on other sites

This topic is 4686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this