By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

initializing heap-allocated structs

P: n/a
Hello,

I can initialized, e.g, heap-allocated ints by writing something like
new int(5).
Now I'd like to know if it is possible to do the same thing for struct
types. Say, I have a struct declared by the following lines:
struct S {
int i;
const char c;
}
I want to write something like
new S {5, 'd'}.
Is this possible?

Wolfgang
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Wolfgang Jeltsch escribió:
types. Say, I have a struct declared by the following lines:
struct S {
int i;
const char c;
}
I want to write something like
new S {5, 'd'}.
Is this possible?


You can add a constructor to S.

S (int i, const char c) : i (i), c (c) { }

And then use
new S (5, 'd');

Regards.
Jul 19 '05 #2

P: n/a
"Wolfgang Jeltsch" <je*****@tu-cottbus.de> wrote in message
news:bh************@ID-77306.news.uni-berlin.de...
Hello,

I can initialized, e.g, heap-allocated ints by writing something like
new int(5).
Now I'd like to know if it is possible to do the same thing for struct
types. Say, I have a struct declared by the following lines:
struct S {
int i;
const char c;
Add a constructor:
S(int _i, char _c) : i(_i), c(_c);
}
I want to write something like
new S {5, 'd'}.
Make that new S(5, 'd');

HTH,
S. Armondi Is this possible?

Wolfgang

Jul 19 '05 #3

P: n/a

"Samuele Armondi" <sa****************@hotmail.com> wrote in message
news:3f********@mk-nntp-1.news.uk.worldonline.com...
"Wolfgang Jeltsch" <je*****@tu-cottbus.de> wrote in message
news:bh************@ID-77306.news.uni-berlin.de...
Hello,

I can initialized, e.g, heap-allocated ints by writing something like
new int(5).
Now I'd like to know if it is possible to do the same thing for struct
types. Say, I have a struct declared by the following lines:
struct S {
int i;
const char c;
Add a constructor:
S(int _i, char _c) : i(_i), c(_c);


Sorry, typo slipped in... That should be:
S(int _i, char _c) : i(_i), c(_c) {};
}
I want to write something like
new S {5, 'd'}.


Make that new S(5, 'd');

HTH,
S. Armondi
Is this possible?

Wolfgang


Jul 19 '05 #4

P: n/a

Wolfgang Jeltsch <je*****@tu-cottbus.de> wrote in message
news:bh************@ID-77306.news.uni-berlin.de...
Hello,

I can initialized, e.g, heap-allocated ints by writing something like
new int(5).
Now I'd like to know if it is possible to do the same thing for struct
types. Say, I have a struct declared by the following lines:
struct S {
int i;
const char c;
}
I want to write something like
new S {5, 'd'}.
Is this possible?


Yes, and the mechanism (knows as a 'constructor') does
not depend upon where the memory is allocated:

#include <iostream>

struct S
{
int i;
const char c;
S(int ii, char cc) : i(ii), c(cc) { } /* constructor */
};

int main()
{
/* automatic storage: */
S obj(5, 'd');
std::cout << obj.i << '\n'; /* prints 5 */
std::cout << obj.c << '\n'; /* prints d */

/* allocated storage: */
S *ptr = new S(5, 'd');
std::cout << ptr->i << '\n'; /* prints 5 */
std::cout << ptr->c << '\n'; /* prints d */

delete ptr;
return 0;
}

Read about constructors in any quality C++ textbook.

-Mike

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.