data member initiatialization question

P

paallikko.rosvo

Hi,

If I have the following passage of code:

class A
{
public:
A() {}
A(int a) {}
};

class B
{
A a;
A b(10);
};

Why is "A a" legal in this context and "A b(10)" is not? Is there any
way to achieve the same thing with a constructor that takes arguments?
I wouldn't like to use naked pointers.

Thanks!
-R
 
R

Rolf Magnus

Hi,

If I have the following passage of code:

class A
{
public:
A() {}
A(int a) {}
};

class B
{
A a;
A b(10);
};

Why is "A a" legal in this context and "A b(10)" is not?

Because you cannot initialize member variables at the point you declare
them.
Is there any way to achieve the same thing with a constructor that takes
arguments?

If you want to initialize members explicitly, you must do that in the
initializer list of B's constructor.
I wouldn't like to use naked pointers.

I don't see any pointers in your code.
 
O

Obnoxious User

Hi,

If I have the following passage of code:

class A
{
public:
A() {}
A(int a) {}
};

class B
{
A a;
A b(10);
};

Why is "A a" legal in this context and "A b(10)" is not? Is there any
way to achieve the same thing with a constructor that takes arguments? I
wouldn't like to use naked pointers.

class A
{
public:
A() {}
A(int a) {}
};

class B
{
A a;
A b;
B() : b(10) {}
};
 
P

paallikko.rosvo

I don't see any pointers in your code.

There aren't at this point :)

what i meant is: is there any other (intelligent) way of doing this
than:

A *b;

and then in the constructor:

b = new A(10);

There's nothing wrong as such in this, but I was wondering if this is
the best way or is there any good way to do this so that the memory
would be freed without a call to delete in the destructor.

Cheers,
-R
 
P

peter koch

Hi,

If I have the following passage of code:

class A
{
public:
A() {}
A(int a) {}

};

class B
{
A a;
A b(10);

};

Why is "A a" legal in this context and "A b(10)" is not? Is there any
way to achieve the same thing with a constructor that takes arguments?
I wouldn't like to use naked pointers.

I would recommend you to read a good C++ book or the C++ faq.
Obviously, you know very little about C++ and in that case, it ís
necessary to get a grasp of the basics first.

Good luck!
Peter
 
B

brian tyler

class A
{
public:
A() {}
A(int a) {}

};

class B
{
A a;
A b;
B() : b(10) {}

};

Or even

class A
{
int a_;

public:
A() {}
A(int a) : a_(a) {}
};

class B
{
A a;
A* b;

public:
B() : b( new A(10) ) {}
};

But if you plan on using pointers then you should find out about smart
pointers (http://www.boost.org/libs/smart_ptr/smart_ptr.htm and
http://msdn2.microsoft.com/en-us/library/ew3fk483(VS.71).aspx)

Different types of smart pointers are used in different situations (ie
depending on if you want to share ownership of the pointer, or you
want to transfer ownership of a pointer for example), they allow you
to use pointers without needing to worry about memory leaks. I
remember reading somewhere that one should try to avoid raw (non-
smart) pointers modern C++, in other words you need to have a good
reason to use one over a smart equivalent (I suppose that reason could
be speed if you need shared ownership).

Brian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top