473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Calling constructor

Let's say I have the following code where a class 'pipo' has 8 instances
of foo:

class foo
{
foo ( const char * _name):
name = _name
{

}
private:
const char * name;
};

class pipo
{
foo *mf[8];
pipo()
{
for (unsigned int i = 0 ; i < 8; ++i)
{
ostringstream ost;
ost << "MY_FOO_" << i;
string name = ost.str();
mf[i] = new foo (name.c_str());
}
}
};

Is it mandatory to do in such a way: using array of pointers on foo. Or
may I do it by using plain array of instances of foo?
In this case, I have no clue how to process to call the constructor with
the correct C like string as defined in the exemple...
Do you have any ideas how to proceed?

Something like this?

class pipo
{
foo mf[8];
pipo ():
????
{

}
};
Regards

Sylvain

Jul 22 '05 #1
5 1658
"Sylvain" <sy*************@aitb.org> wrote...
Let's say I have the following code where a class 'pipo' has 8 instances
of foo:

class foo
{
foo ( const char * _name):
name = _name
{

}
private:
const char * name;
};

class pipo
{
foo *mf[8];
pipo()
{
for (unsigned int i = 0 ; i < 8; ++i)
{
ostringstream ost;
ost << "MY_FOO_" << i;
string name = ost.str();
mf[i] = new foo (name.c_str());
}
}
};

Is it mandatory to do in such a way: using array of pointers on foo. Or
may I do it by using plain array of instances of foo?
In this case, I have no clue how to process to call the constructor with
the correct C like string as defined in the exemple...
Do you have any ideas how to proceed?

Something like this?

class pipo
{
foo mf[8];
pipo ():
????
{

}
};


There is no way to directly initialise an array if the array is
a non-static member of a class. Even with your 'array of pointers'
solution, the pointers are uninitialised in the beginning and then
are _assigned_ values in the constructor body. It's a shortcoming
of the language, nothing you or anybody else can do about it at
this time.

Victor
Jul 22 '05 #2

"Sylvain" <sy*************@aitb.org> wrote in message news:40**********************@dreader2.news.tiscal i.nl...
Is it mandatory to do in such a way: using array of pointers on foo. Or
may I do it by using plain array of instances of foo?
You can make a plain array:
foo mf[8];
or a dynmaic array
foo* mf = new foo[8];

In this case, I have no clue how to process to call the constructor with
the correct C like string as defined in the exemple...


Unfortunately, arrays are bastard type and initializing them with something
other than the default is frequently problematic.

In your case, I'd just allocate them all with
foo mf[8];
and then provide a "SetName(const char*)" method or something to provide
the name after they are constructed.

Jul 22 '05 #3
"Sylvain" <sy*************@aitb.org> wrote in message
news:40**********************@dreader2.news.tiscal i.nl...
Let's say I have the following code where a class 'pipo' has 8 instances
of foo:

class foo
{
foo ( const char * _name):
name = _name
what is this? ITYM name(_name)
{

}
private:
const char * name;
};


If you are planning to put class instances in a container it is wise to
include a default constructor:

class foo
{
public:
foo() : name(0) {}
foo ( const char * _name) : name(_name) {}
private:
const char * name;
};

Then to make an array:

foo mf[8];
for (int j = 0; j < 8; ++j)
mf[j] = foo("abc");

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4
On Thu, 12 Feb 2004 20:00:24 +0100, Sylvain <sy*************@aitb.org> wrote:
Let's say I have the following code where a class 'pipo' has 8 instances
of foo:

class foo
{
foo ( const char * _name):
name = _name
{

}
private:
const char * name;
};

Class 'foo' has one very big assumption built-in:
* The lifetime of the character array that is the 'name' of a 'foo'
instance must include the lifetime of the 'foo' instance.
Otherwise the 'foo' instance may be referring to some deallocated
or otherwise cleaned-up memory, no longer a meaningful 'name'.

class pipo
{
foo *mf[8];
pipo()
{
for (unsigned int i = 0 ; i < 8; ++i)
{
ostringstream ost;
ost << "MY_FOO_" << i;
string name = ost.str();
mf[i] = new foo (name.c_str());
Here the big 'foo' assumption is violated.

The 'foo' constructor is passed a pointer to the internal memory
of a 'string' instance.

Shortly after that 'string' instance goes out of scope and deallocates
the memory now pointed to by the 'foo' instance.
}
}
};

Is it mandatory to do in such a way: using array of pointers on foo.
No.

Or may I do it by using plain array of instances of foo?
Yes.

You can even use a 'std::vector', which is a good habit to get into
(even if it buys you nothing but overhead in this particular case).

In this case, I have no clue how to process to call the constructor with
the correct C like string as defined in the exemple...


In the 'foo' constructor you should _copy_ the string.

The easiest way to do that is to use a 'std::string' as member,
instead of a 'char const*'.

When you use 'std::string' you don't have to deal with allocation and
deallocation.

Jul 22 '05 #5
"Sylvain" <sy*************@aitb.org> wrote in message
news:40**********************@dreader2.news.tiscal i.nl
Let's say I have the following code where a class 'pipo' has 8
instances of foo:

class foo
{
foo ( const char * _name):
name = _name
{

}
private:
const char * name;
};

class pipo
{
foo *mf[8];
pipo()
{
for (unsigned int i = 0 ; i < 8; ++i)
{
ostringstream ost;
ost << "MY_FOO_" << i;
string name = ost.str();
mf[i] = new foo (name.c_str());
}
}
};

Is it mandatory to do in such a way: using array of pointers on foo.
Or may I do it by using plain array of instances of foo?
In this case, I have no clue how to process to call the constructor
with the correct C like string as defined in the exemple...
Do you have any ideas how to proceed?

Something like this?

class pipo
{
foo mf[8];
pipo ():
????
{

}
};
Regards

Sylvain

You could construct the array outside of pipo and have pipo just store a
pointer to it, e.g.,

#include <string>
using namespace std;

class foo
{
public:
foo (const char * _name) : name(_name)
{}
private:
string name;
};

class pipo
{
public:
foo *pmf;
pipo(foo *pfoo) : pmf(pfoo)
{}
};
int main()
{
foo scratch[8] = {"MY_FOO_0", "MY_FOO_1", "MY_FOO_2", "MY_FOO_3",
"MY_FOO_4", "MY_FOO_5", "MY_FOO_6", "MY_FOO_7"};
pipo p(scratch);
return 0;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Murat Tasan | last post by:
i have a quick question... is there a way to obtain the reference to the object which called the currently executing method? here is the scenario, i have a class and a field which i would like to...
9
by: Giulio | last post by:
why definition of two constructors like these is not possible in c++??? ----------------------- date::date(const int d, const int m, const int y, const int ora, const int mi, const int se){...
2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
8
by: Greg Bacchus | last post by:
I have a base class with a method that is to be called in the constructor of the inheritting classes. Is there any way of determining, say, the Type of the class that is calling it. e.g. ...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
1
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.