6 Replies - 253 Views - Last Post: 15 August 2012 - 02:12 AM Rate Topic: -----

#1 nunc  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 123
  • Joined: 20-November 11

Other objects in Headers

Posted 14 August 2012 - 07:41 PM

Hey DIC. I've used C++ for a few months now and this is the first time I've come across this problem.


class A
{
private:
B b(5);
};


From what I understand I cannot initialize b in the header. I could use a pointer to b and initialize it in the .cpp file, but I really don't need b outside the scope of this class. I could always create a temporary b object in the .cpp and override the equals operator, but is there another way to do this? Can you initialize objects in header files?

Is This A Good Question/Topic? 0
  • +

Replies To: Other objects in Headers

#2 pokiaka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Re: Other objects in Headers

Posted 14 August 2012 - 07:51 PM

You should initialize b in your constructor.
It will still be scoped to your class.

The problem is not that you're trying to initialize in your header files (it won't be good practice, but it will be allowed). it's because it's in your class declaration.

There are some exceptions, for example, you can initialize constant static integers.

This post has been edited by pokiaka: 14 August 2012 - 07:55 PM

Was This Post Helpful? 1
  • +
  • -

#3 nunc  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 123
  • Joined: 20-November 11

Re: Other objects in Headers

Posted 14 August 2012 - 07:56 PM

So you're saying I can declare it in the header, and initialize it in the constructor? How would I go about that? :o
Was This Post Helpful? 0
  • +
  • -

#4 pokiaka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Re: Other objects in Headers

Posted 14 August 2012 - 07:59 PM

Inline example:

class A
{
private:
B b;

public:
	A()
	{
		b = 5;
	}
};


As you can guess, this is typical behavior for default constructors.

This post has been edited by pokiaka: 14 August 2012 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: Other objects in Headers

Posted 14 August 2012 - 08:55 PM

Would using the initialization list work?

class A
{
  private:
    B b;

  public:
    A() : b(5) {}
};


Was This Post Helpful? 1
  • +
  • -

#6 pokiaka  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 76
  • Joined: 05-August 11

Re: Other objects in Headers

Posted 15 August 2012 - 12:49 AM

View Post#define, on 14 August 2012 - 08:55 PM, said:

Would using the initialization list work?

class A
{
  private:
    B b;

  public:
    A() : b(5) {}
};



I choose to put it inside a body because I find having initialization list and no body just ugly.
In a separate cpp file it looks something like this:

A::A
  : a(a), b(B)/>
{}

This post has been edited by pokiaka: 15 August 2012 - 12:53 AM

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,279
  • Joined: 25-December 09

Re: Other objects in Headers

Posted 15 August 2012 - 02:12 AM

Using initialization lists should be preferred over initializing inside the function body. There are several benefits of using the initialization list, the biggest reason being avoiding calling the default constructor and the copy constructor for the same object. Also you are required to use initialization lists to properly construct variables using const or references. See this link for more information as to why you should be using initialization lists: C++ General: What is the initialization list and why should I use it? and Initialization lists.

Jim
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1