खोज…


कक्षाएं

एक नामित वर्ग या संरचना के विपरीत, अनाम वर्ग और संरचना को तत्काल परिभाषित किया जाना चाहिए जहां वे परिभाषित हैं, और निर्माणकर्ता या विध्वंसक नहीं हो सकते।

struct {
    int foo;
    double bar;
} foobar;

foobar.foo = 5;
foobar.bar = 4.0;

class {
    int baz;
public:
    int buzz;
    
    void setBaz(int v) {
        baz = v;
    }
} barbar;

barbar.setBaz(15);
barbar.buzz = 2;

अनाम सदस्य

सी ++ के लिए एक गैर-मानक विस्तार के रूप में, सामान्य संकलक अनाम सदस्यों के रूप में कक्षाओं के उपयोग की अनुमति देते हैं।

struct Example {
    struct {
        int inner_b;
    };
    
    int outer_b;
    
    //The anonymous struct's members are accessed as if members of the parent struct
    Example() : inner_b(2), outer_b(4) {
        inner_b = outer_b + 2;
    }
};

Example ex;

//The same holds true for external code referencing the struct
ex.inner_b -= ex.outer_b;

एक प्रकार के उपनाम के रूप में

बेनाम वर्ग प्रकार भी जब प्रकार उपनाम यानी के माध्यम से बनाने, इस्तेमाल किया जा सकता typedef और using :

सी ++ 11
using vec2d = struct {
    float x;
    float y;
};
typedef struct {
    float x;
    float y;
} vec2d;
vec2d pt;
pt.x = 4.f;
pt.y = 3.f;

अनाम संघ

एक अनाम संघ के सदस्य नाम संघ घोषणा के दायरे से संबंधित हैं जो इस दायरे के अन्य सभी नामों के लिए अलग होना चाहिए। यहां उदाहरण में एक ही निर्माण है उदाहरण " बेनामी " का उपयोग करते हुए बेनामी सदस्य लेकिन मानक अनुरूप है।

struct Sample {
    union {
        int a;
        int b;
    };
    int c;
};
int main()
{
  Sample sa;
  sa.a =3;
  sa.b =4;
  sa.c =5;
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow