2012-12-13 23 views
6

से बहुत ही सरल प्रश्न। क्या यह मान्य सी ++ 11 है?गैर-स्थैतिक सदस्य प्रारंभकर्ता अन्य गैर-स्थिर

struct Foo { 
    int bar = 1; 
    int baz = bar; 
}; 

जीसीसी (4.7.2) और बजना (3.1) दोनों पंडिताऊ सेटिंग्स के साथ यह स्वीकार करते हैं:

-std=c++11 -Wall -W -pedantic

इंटेल सी ++ (13.0.1.117) नहीं करता है।

error: a nonstatic member reference must be relative to a specific object

कौन सही है: यह साथ int baz = bar; पर भौंकता?

मामले में आप आश्चर्य है, मैं इस तरह कोड है, जहां यह प्रवर्तन कोड लाता है एक साथ करीब है, बल्कि निर्माता में अंतिम पंक्ति चलती से के लिए इस का उपयोग करें:

uint8_t colorR = -1; 
uint8_t colorG = -1; 
uint8_t colorB = -1; 
uint8_t colorA = -1; 
GLubyte RGBAVec[4] = {colorR, colorG, colorB, colorA}; 

उत्तर

3

5.1p12 An id-expression that denotes a non-static data member or non-static member function of a class can only be used:

  • as part of a class member access (5.2.5) in which the object expression refers to the member’s class or a class derived from that class, or
  • to form a pointer to member (5.3.1), or
  • in a mem-initializer for a constructor for that class or for a class derived from that class (12.6.2), or
  • in a brace-or-equal-initializer for a non-static data member of that class or of a class derived from that class (12.6.2), or
  • if that id-expression denotes a non-static data member and it appears in an unevaluated operand.

तो हाँ, यह है:

struct Foo { 
    int bar = 1; 
    int baz = bar; 
}; 

वैध सी ++ 11 है।

लेकिन आदेश के बारे में सावधान किया है क्योंकि:

12.6.2p10 In a non-delegating constructor, initialization proceeds in the following order:

  • First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
  • Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
  • Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
  • Finally, the compound-statement of the constructor body is executed

तो Non-static data member initializers proposal (समस्या 3) में निर्दिष्ट है:

A third issue is that class-scope lookup could turn a compile-time error into a run-time error:

struct S { 
    int i = j; // ill-formed without forward lookup, undefined behavior with 
    int j = 3; 
}; 

(Unless caught by the compiler, i might be intialized with the undefined value of j.)

+0

धन्यवाद। यह निश्चित दिखता है। हालांकि "उस वर्ग से प्राप्त कक्षा के" के साथ क्या मतलब है? व्युत्पन्न वर्ग के सदस्य के साथ सदस्य को प्रारंभ करना कैसे संभव है? व्युत्पन्न वर्ग अभी तक घोषित नहीं किया गया है, इसलिए इसे एक्सेस करना संभव नहीं है। –

+0

@ निकोस सी "या उस वर्ग से व्युत्पन्न कक्षा का" एक "ब्रेस-या-बराबर-प्रारंभकर्ता" को संदर्भित करता है जिसका अर्थ है कि प्रारंभकर्ता एक घिरा हुआ वर्ग में होता है। असल में मुझे लगता है कि इसका मतलब है कि आप अपने माता-पिता वर्गों के गैर स्थैतिक डेटा सदस्य के साथ गैर स्थैतिक डेटा सदस्यों को प्रारंभ कर सकते हैं। यह एक अन्य तरीका है जिसे आप समझते हैं, जो अधिक समझ में आता है :) – Drax

संबंधित मुद्दे