Working of nested class objects?

user3693825

I am trying to understand the order of exectution of a class which has nested objects of another class inside it. Here's my simple program :

#include<iostream>
#include <string>
using namespace std;
class Alpha
{
    int a;
    public:
    Alpha(int x)
    {
        a=x;
    }
};
class Beta
{   int b;
    public:
    Beta(int y)
    {
        b=y;
    }

};
class Gamma
{
    Alpha A;
    Beta B;
    int c;
    public:
    Gamma(int a,int b, int d): A(a), B(b)
    {
        c=d;
    }
};
void main()
{
    Gamma g(5,6,7);
}

As you can see, Gamma has 2 nested objects. Now when the first line of main() is executed, how does the execution start inside the class Gamma? The constructer is called first or the objects/data-members are created first?

Sergey Kalinichenko

The question of constructor execution order is simple: first, Gamma constructor starts, but then it immediately proceeds to initializing Alpha and Beta, as specified in your initialier list. Once the intializer list is done, the body of Gamma's constructor is executed.

There is an important twist to this: C++ will initialize nested objects in the order in which they are declared in the class, not in the order in which they are listed in the initializer list. In other words, Alpha will be initialized ahead of Beta even if you reverse A(a) and B(b):

// The compiler will issue a warning for this
Gamma(int a,int b, int d): B(b), A(a)
{
    c=d;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Working of nested class objects?

From Dev

Javax validation on nested objects - not working

From Dev

Eloquent hasMany relationship get() not working for nested objects

From Dev

meteor / minimongo working with nested data objects

From Dev

Inject not working for nested objects[Jersey 2.22.1]

From Dev

Eloquent hasMany relationship get() not working for nested objects

From Dev

ElasticSearch querying nested objects not working as expected

From Dev

Referencing nested class objects in C#

From Dev

Count duplicate objects in nested class by common field

From Dev

Save Nested POJO class Objects using hibernate

From Dev

Onactivityresult is not working fine in nested fragment class android

From Dev

Static objects of nested class can be declared without class definition

From Dev

Nested class AsyncTask can't modify Outer Class static objects

From Dev

Typeahead v0.10.2 & Bloodhound - Working With Nested JSON Objects

From Dev

Using nested foreach loops to unset objects in array not working as expected?

From Dev

Using nested foreach loops to unset objects in array not working as expected?

From Dev

Azure mongodb query on nested collection of objects not working as expected

From Dev

Companion class requires import of Companion object methods and nested objects?

From Dev

Creating class objects using LINQ with a highly nested XML

From Dev

React-Router active class is not working for nested routes

From Dev

React-Router active class is not working for nested routes

From Dev

Class="dropdown" and attribute data-dropdown-menu not working with nested ul

From Dev

How to best store data from CSV in java class? A single list of Row objects, or a single object with nested objects?

From Dev

Working with Objects

From Dev

Is this a Nested class?

From Dev

Computed destructuring of nested Objects

From Dev

Docxtemplater. Nested objects

From Dev

Nested Objects in Realm

From Dev

Serializer containing nested objects

Related Related

  1. 1

    Working of nested class objects?

  2. 2

    Javax validation on nested objects - not working

  3. 3

    Eloquent hasMany relationship get() not working for nested objects

  4. 4

    meteor / minimongo working with nested data objects

  5. 5

    Inject not working for nested objects[Jersey 2.22.1]

  6. 6

    Eloquent hasMany relationship get() not working for nested objects

  7. 7

    ElasticSearch querying nested objects not working as expected

  8. 8

    Referencing nested class objects in C#

  9. 9

    Count duplicate objects in nested class by common field

  10. 10

    Save Nested POJO class Objects using hibernate

  11. 11

    Onactivityresult is not working fine in nested fragment class android

  12. 12

    Static objects of nested class can be declared without class definition

  13. 13

    Nested class AsyncTask can't modify Outer Class static objects

  14. 14

    Typeahead v0.10.2 & Bloodhound - Working With Nested JSON Objects

  15. 15

    Using nested foreach loops to unset objects in array not working as expected?

  16. 16

    Using nested foreach loops to unset objects in array not working as expected?

  17. 17

    Azure mongodb query on nested collection of objects not working as expected

  18. 18

    Companion class requires import of Companion object methods and nested objects?

  19. 19

    Creating class objects using LINQ with a highly nested XML

  20. 20

    React-Router active class is not working for nested routes

  21. 21

    React-Router active class is not working for nested routes

  22. 22

    Class="dropdown" and attribute data-dropdown-menu not working with nested ul

  23. 23

    How to best store data from CSV in java class? A single list of Row objects, or a single object with nested objects?

  24. 24

    Working with Objects

  25. 25

    Is this a Nested class?

  26. 26

    Computed destructuring of nested Objects

  27. 27

    Docxtemplater. Nested objects

  28. 28

    Nested Objects in Realm

  29. 29

    Serializer containing nested objects

HotTag

Archive