473,406 Members | 2,713 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,406 software developers and data experts.

Revisiting how to implement __property in ansi c++

2
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 1127
weaknessforcats
9,208 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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
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
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
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
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
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
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
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
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
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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,...

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.