473,386 Members | 1,763 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,386 software developers and data experts.

Call Constructor for object in struct

Hello,

How do I call the constructor when a class object (non-pointer) is in a
structure?

struct {
...
CMyClass C;
} MyStruct;

MyStruct A, B;

TIA!!
Sep 29 '08 #1
9 4688
David wrote:
How do I call the constructor when a class object (non-pointer) is in a
structure?

struct {
...
CMyClass C;
} MyStruct;

MyStruct A, B;

TIA!!
What do you mean by "call a constructor"? What constructors does
'CMyClass' have? Perhaps this is going to help:

class CMyClass {
public:
CMyClass(int);
CMyClass(char const*);
};

...

MyStruct A = { ..., CMyClass(42) }, B = { ..., CMyClass("David") };

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 29 '08 #2
David wrote:
Hello,

How do I call the constructor when a class object (non-pointer) is in a
structure?

struct {
...
CMyClass C;
} MyStruct;

MyStruct A, B;
You write a constructor in your struct which calls the constructor of
the member class.
Sep 29 '08 #3
On Sep 29, 5:04*pm, Juha Nieminen <nos...@thanks.invalidwrote:
David wrote:
Hello,
How do I call the constructor when a class object (non-pointer) is in a
structure?
struct {
* ...
* CMyClass C;
} MyStruct;
MyStruct A, B;

* You write a constructor in your struct which calls the constructor of
the member class.
I wonder why C++ is in favor of CMyClass(char const*); copy
constructor then CMyClass(char const &);

Any ideas?
Sep 29 '08 #4
puzzlecracker wrote:
[..]
I wonder why C++ is in favor of CMyClass(char const*); copy
constructor then CMyClass(char const &);

Any ideas?
Please rephrase your statement about C++ and favors. A copy constructor
for the 'CMyClass' class would be either

CMyClass(CMyClass const &);

or

CMyClass(CMyClass &);

Anything else is *not* a copy constructor.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 29 '08 #5
On Sep 29, 5:25*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
puzzlecracker wrote:
[..]
I wonder why *C++ is in favor of *CMyClass(char const*); copy
constructor *then * * *CMyClass(char const &);
Any ideas?

Please rephrase your statement about C++ and favors. *A copy constructor
for the 'CMyClass' class would be either

* * *CMyClass(CMyClass const &);

or

* * *CMyClass(CMyClass &);

Anything else is *not* a copy constructor.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
No need to rephrase, you answered it. I'm assuming, by providing one
of (pointer or reference version) copy ctors, the other one will not
be generated by compiler by default. Is my assumption correct, aka
reflected in the standard?
Sep 29 '08 #6
puzzlecracker wrote:
On Sep 29, 5:25 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>puzzlecracker wrote:
>>[..]
I wonder why C++ is in favor of CMyClass(char const*); copy
constructor then CMyClass(char const &);
Any ideas?
Please rephrase your statement about C++ and favors. A copy constructor
for the 'CMyClass' class would be either

CMyClass(CMyClass const &);

or

CMyClass(CMyClass &);

Anything else is *not* a copy constructor.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

No need to rephrase, you answered it. I'm assuming, by providing one
of (pointer or reference version) copy ctors, the other one will not
be generated by compiler by default. Is my assumption correct, aka
reflected in the standard?
Erm... There is *no* "pointer version copy ctor". Copy constructors
*always* take a reference argument. And, no, if you define some other
constructor, the compiler will still provide a copy constructor in the
former interface, if it can (depends whether the members are copyable).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 29 '08 #7
On Sep 29, 10:47 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
David wrote:
How do I call the constructor when a class object
(non-pointer) is in a structure?
struct {
...
CMyClass C;
} MyStruct;
MyStruct A, B;
What do you mean by "call a constructor"?
According to the standard, you can't "call a constructor:-)".
At any rate, what he probably wants is to initialize the
elements.
What constructors does 'CMyClass' have? Perhaps this is going
to help:
class CMyClass {
public:
CMyClass(int);
CMyClass(char const*);
};
...
MyStruct A = { ..., CMyClass(42) }, B = { ..., CMyClass("David")};
Note that in this case, you don't even need the CMyClass. If
the constructors take a single argument, and aren't declared
explicit, explicit conversion will take place.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 30 '08 #8
On Sep 29, 11:25 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:

[...]
Please rephrase your statement about C++ and favors. A copy constructor
for the 'CMyClass' class would be either
CMyClass(CMyClass const &);
or
CMyClass(CMyClass &);
Anything else is *not* a copy constructor.
MyClass( MyClass volatile& ) ;
MyClass( MyClass const volatile& ) ;

:-).

(Of course, I've never found a use for volatile except on very
basic types, and it doesn't work with most modern compilers
anyway. But formally...)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 30 '08 #9
Yeah, basically that... Would this work:

struct {
int a,b,c,d,e,f,g,h,i,j;
CMyClass C;
} MyStruct;

MyStruct A={CMyClass(whatever))};

or would I have to always deal with a through j?

How about:

struct {
int a,b,c,d,e,f,g,h,i,j;
CMyClass C;

void Init(void) {
a=b=c=d=e=f=g=h=i=j=0;
C(whatever)
}
} MyStruct;

MyStruct A;

A.Init();

??

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:gb**********@news.datemas.de...
David wrote:
>How do I call the constructor when a class object (non-pointer) is in a
structure?

struct {
...
CMyClass C;
} MyStruct;

MyStruct A, B;

TIA!!

What do you mean by "call a constructor"? What constructors does
'CMyClass' have? Perhaps this is going to help:

class CMyClass {
public:
CMyClass(int);
CMyClass(char const*);
};

...

MyStruct A = { ..., CMyClass(42) }, B = { ..., CMyClass("David") };

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Sep 30 '08 #10

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

Similar topics

13
by: Mike Qin | last post by:
Hi there, Now I'm writing code mainly in C++. And some existing c code will call some functions which are in a C++ class. I read the basic techniques introduced from a SUN's web site. Basically...
34
by: Jason Heyes | last post by:
Is there a special name for a constructor that does struct-like initialisation? Example: class DefaultCtor { std::string class_name; bool compiler_generated; public:...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
6
by: syntheticpp | last post by:
Is the program at the end a correct C++ program? I ask because the output is: B::operator A&() A::A() B::B() B::~B() A::~A()
9
by: Andy | last post by:
Hi, is it in C++ possible to call a constructor from another constructor? In Java you can do the following: public class MyClass { public MyClass() { this(10);
18
by: cmk128 | last post by:
hi 1 #include <stdio.h> 2 3 class A{ 4 private: 5 int x; 6 public: 7 A(int x){ 8 this->x;
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
11
by: dolphin | last post by:
Hi All! I have a question that how to call a function just using a string. For example There is a .cpp file named a.cpp.There are some functions::fun1() fun2() fun3(). I have another fucntion...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.