Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ in-class member initialization

Reply
Thread Tools

C++ in-class member initialization

 
 
Syron
Guest
Posts: n/a
 
      05-17-2011
Before I fell asleep last night, I had the following idea for in-class member initialization with no runtime overhead. What do you think?

#define INCLASS_INIT(ctype, name, ...) \
class _ ## name ## _INIT { \
private: ctype m_data; \
public: \
inline _ ## name ## _INIT() : \
m_data(__VA_ARGS__) {} \
inline _ ## name ## _INIT(const ctype& v) : \
m_data(v) {} \
inline operator ctype&() \
{ return m_data; } \
inline operator const ctype&() const \
{ return m_data; } \
inline ctype* operator&() \
{ return &m_data; } \
inline const ctype* operator&() const \
{ return &m_data; } \
inline ctype& operator=(const ctype& v) \
{ return m_data=v; } \
} name

// Usage:
class Foo {
public:
INCLASS_INIT(int, m_v1, 1);
INCLASS_INIT(int, m_v2, 2);
Foo() : m_v2(3)
{}
};
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-17-2011
On 5/17/2011 1:06 AM, Syron wrote:
> Before I fell asleep last night, I had the following idea for in-class member initialization with no runtime overhead. What do you think?
>
> #define INCLASS_INIT(ctype, name, ...) \
> class _ ## name ## _INIT { \
> private: ctype m_data; \
> public: \
> inline _ ## name ## _INIT() : \
> m_data(__VA_ARGS__) {} \
> inline _ ## name ## _INIT(const ctype& v) : \
> m_data(v) {} \
> inline operator ctype&() \
> { return m_data; } \
> inline operator const ctype&() const \
> { return m_data; } \
> inline ctype* operator&() \
> { return&m_data; } \
> inline const ctype* operator&() const \
> { return&m_data; } \
> inline ctype& operator=(const ctype& v) \
> { return m_data=v; } \
> } name
>
> // Usage:
> class Foo {
> public:
> INCLASS_INIT(int, m_v1, 1);
> INCLASS_INIT(int, m_v2, 2);
> Foo() : m_v2(3)


This is confusing. What's the value of m_v2? I see it initialized to
'3' here, but is that what I'd see when I try using it elsewhere?

> {}
> };


Curious (like a two-headed calf), but what's the point? Also, is it
intentionally limited to simple types? For instance, you can't declare
a reference that way, can you?

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Krice
Guest
Posts: n/a
 
      05-18-2011
On 17 touko, 08:06, Syron <(E-Mail Removed)> wrote:
> What do you think?


I think it looks stupid and confusing. Well, better luck next time..
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
error C2614: 'CYYYRegister' : illegal member initialization:'CRequest' is not a base or member Angus C++ 1 03-06-2008 11:38 AM
How to initialize an array member in the member initialization list? jut_bit_zx@eyou.com C++ 3 10-10-2005 12:10 AM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments