473,480 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Revisiting how to implement __property in ansi c++

2 New Member
I have the same problem as discussed at

http://bytes.com/topic/c/answers/133...roperty-ansi-c

The best solution there looks like this one, but I cannot get it to compile - see embedded comments. Any advice much appreciated.

Expand|Select|Wrap|Line Numbers
  1. template <typename T, typename C>
  2.  class Property
  3.  {
  4.  typedef T (C::*Get)() const;
  5.  typedef void (C::*Set)(T);
  6.  Get GetFunc_;
  7.  Set SetFunc_;
  8.  C & Class_;
  9.  public:
  10.  Property(Get GetFunc, Set SetFunc, C &Class)
  11.  : GetFunc_(GetFunc), SetFunc_(SetFunc), Class_(Class) {}
  12.  
  13.  operator T () const
  14.  {
  15.  return (Class_.*GetFunc_)();
  16.  }
  17.  
  18.  Property<T, C>& operator=(T val)
  19.  {
  20.  (Class_.*SetFunc_)(val);
  21.  return *this;
  22.  }
  23.  };
  24.  
  25.  class TMyClass
  26.  {
  27.  int FMyVariable;
  28.  public:
  29.  Property<int, TMyClass> MyVariable;
  30.  // fails at the line below in an MFC compile using VS2013
  31.  // wants argument lists, so I changed to
  32.  // TMyClass() : MyVariable(GetMyVariable(), SetMyVariable(int), *this)
  33.  // but no good.
  34.  TMyClass() : MyVariable(GetMyVariable, SetMyVariable, *this)
  35.  {
  36.  }
  37.  int GetMyVariable() const
  38.  {
  39.  return FMyVariable;
  40.  }
  41.  void SetMyVariable(int Value)
  42.  {
  43.  FMyVariable = Value;
  44.  }
  45.  };
Jul 19 '14 #1
3 1128
weaknessforcats
9,208 Recognized Expert Moderator Expert
This is a problem:

Expand|Select|Wrap|Line Numbers
  1. TMyClass() : MyVariable(GetMyVariableSetMyVariable, *this)
  2.  {
  3.  }
  4.  int GetMyVariable() const
  5.  {
  6.  return FMyVariable;
  7.  }
  8.  void SetMyVariable(int Value)
  9.  { 
  10.  FMyVariable = Value;
  11.  }
  12.  
Here two function pointers are being used before the compiler has seen the functions.

Often the trick to templates is to code the classes without using templates until it compiles and links. Then convert the class declarations to templates.
Jul 19 '14 #2
DaleC
2 New Member
Hi weakness,

Thanks for the suggestion. Some further googling found a solution. If I change TMyClass constructor to

<code>
class TMyClass
{
int FMyVariable;
public:
Property<int, TMyClass3> MyVariable;
TMyClass() : FMyVariable(0), MyVariable(&TMyClass::GetMyVariable, &TMyClass::SetMyVariable, *this) {}
int GetMyVariable() const
{
return FMyVariable;
}
void SetMyVariable(int Value)
{
FMyVariable = Value;
}
};
</code>

then it compiles and works.
Jul 21 '14 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
OK.

However, in looking at your code it looks like you have a Visitor design pattern in disguise.

Read this: http://bytes.com/topic/c/insights/67...tterns-visitor

Let me know what you think.
Jul 21 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

27
2632
by: Dave Anderson | last post by:
Last September, some of us engaged in a discussion of freeing resources in ASP scripts written in JScript. It can be seen here: http://tinyurl.com/2flzt I recently read Eric Lippert's article on...
1
2104
by: MW | last post by:
Hi, In my ATL COM project I want do implement dialog and add some property pages on it. What should I do do to this ?. I tried to add to standard ATL dialog class some interfaces which allow...
6
3797
by: Charles Law | last post by:
This is going to seem like a basic OO question, but it comes up and bites me every now and again. Suppose we have a multi-tiered protocol to implement, what is the logical, OO way to design the...
7
4784
by: | last post by:
Borland dumped all its "Borand C++ Builder" (BCB) customers. So it is our term to dump Borland (not only BCB). As a part of my attempt to dump long-loved BCB I'm trying to investigate how one can ...
1
1581
by: Ron Adam | last post by:
I was trying to see if I can implement property groups so I can set and pass arguemts as dictionaries. I think this will simplify interfacing to multiple objects and funtions that use a lot of...
100
6809
by: Roose | last post by:
Just to make a tangential point here, in case anyone new to C doesn't understand what all these flame wars are about. Shorthand title: "My boss would fire me if I wrote 100% ANSI C code" We...
2
6774
by: larzeb | last post by:
I receive the following error from the compiler: ScheduledInfo' must implement 'Property AddrLine1() As String' for interface 'ICASSBatch'. Implementing property must have matching...
83
11526
by: sunny | last post by:
Hi All What is C99 Standard is all about. is it portable, i mean i saw -std=C99 option in GCC but there is no such thing in VC++.? which one is better ANSI C / C99? can i know the major...
7
4838
by: Paul Connolly | last post by:
char *s = "Hello"; s = 'J'; puts(s); might print "Jello" in a pre-ANSI compiler - is the behaviour of this program undefined in any pre-ANSI compiler - or would it always have printed "Jello"...
8
2047
AmberJain
by: AmberJain | last post by:
HELLO, Is it necessary for a C programmer to have an ANSI C standard or it's sufficient to own Kernigham and Rithie's The C programming language? I know that the ritchie's book is quite brief...
0
6918
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7102
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...
1
6756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5357
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1310
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.