4

Let's say I have two classes: Box, Circle.

class Box{
int x, y;
...Box(int xcoord, int ycoord){printf("I'm a box."); x = xcoord; y = ycoord;}
};

class Circle{
...Circle(){printf("I'm a circle.");}
};

But let's say in the Circle class, I want to make an instance of the Box class. Well I tried this:

class Circle{
Box b(0,0);
...Circle(){printf("I'm a circle.");}
};

I get an error:

error C2059: syntax error : 'constant'

1

2 Answers 2

6
class Circle {
    Box b;

public:
    Circle() : b(0, 0)
    {
        printf("I'm a circle.");
    }
};
Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain what you are doing in the constructor? Circle() : b(0, 0) ??
You have to initialize the variables in the constructor.
Also, the Box constructor needs to be made public.
@Init: The : b(0, 0) is a member initializer. Members variables must be initialized in a constructor, not in the declaration. One should prefer member initialization to assignment in the constructor body.
5

You are not allowed to instantiate member variables in the class declaration. The reasoning is the member variables should not be used until the item is constructed anyway, hence why they must be instantiated in a constructor.

The code from the semicolon to the opening brace of the constructor, Box() : x(0),y(0) {} is called an initializer list and is used to initialize the variables to default values before the code in the constructor's block is called. If variables are not initialized in this list, C++ will call the no-argument constructor to initialize them (or in the case of built-in data types, do nothing). You did not specify a no-argument constructor for the Box class so it remained uninitialized in the circle class which caused the error. There are two obvious ways you can go about fixing this, either define a no-argument constructor for Box, or initialize the Box member variable in the circle constructor's initializer list. The second method is always preferred.

Using initializer lists in constructors is a good habit to develop. If you wait to initialize big objects in the constructor's code block, you are effectively paying for the construction twice since you are first calling the objects no-argument constructor before entering the code-block and then again calling a constructor to initialize the variable to the state you want.

class Box {
    public:
        int x,y;
        Box(int xcoord, int ycoord){printf("I'm a box."); x = xcoord; y = ycoord;}
        // Box() : x(0), y(0) {} Can do this, not advised.
};

class Circle{
   Box b;
   Circle() : b(0,0) {printf("I'm a circle.");}
};

1 Comment

+1 for pointing out member object will be initialized before entering code-bolck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.