Connecting Tech Pros Worldwide Help | Site Map

initializing heap-allocated structs

Wolfgang Jeltsch
Guest
 
Posts: n/a
#1: Jul 19 '05
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
Julián Albo
Guest
 
Posts: n/a
#2: Jul 19 '05

re: initializing heap-allocated structs


Wolfgang Jeltsch escribió:
[color=blue]
> 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?[/color]

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.
Samuele Armondi
Guest
 
Posts: n/a
#3: Jul 19 '05

re: initializing heap-allocated structs


"Wolfgang Jeltsch" <jeltsch@tu-cottbus.de> wrote in message
news:bhblvh$vocjc$1@ID-77306.news.uni-berlin.de...[color=blue]
> 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;[/color]

Add a constructor:
S(int _i, char _c) : i(_i), c(_c);
[color=blue]
> }
> I want to write something like
> new S {5, 'd'}.[/color]

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

HTH,
S. Armondi[color=blue]
> Is this possible?
>
> Wolfgang[/color]


Samuele Armondi
Guest
 
Posts: n/a
#4: Jul 19 '05

re: initializing heap-allocated structs



"Samuele Armondi" <sammyboyuk_NOSPAM_@hotmail.com> wrote in message
news:3f395f34_3@mk-nntp-1.news.uk.worldonline.com...[color=blue]
> "Wolfgang Jeltsch" <jeltsch@tu-cottbus.de> wrote in message
> news:bhblvh$vocjc$1@ID-77306.news.uni-berlin.de...[color=green]
> > 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;[/color]
>
> Add a constructor:
> S(int _i, char _c) : i(_i), c(_c);[/color]

Sorry, typo slipped in... That should be:
S(int _i, char _c) : i(_i), c(_c) {};
[color=blue]
>[color=green]
> > }
> > I want to write something like
> > new S {5, 'd'}.[/color]
>
> Make that new S(5, 'd');
>
> HTH,
> S. Armondi[color=green]
> > Is this possible?
> >
> > Wolfgang[/color]
>
>[/color]


Mike Wahler
Guest
 
Posts: n/a
#5: Jul 19 '05

re: initializing heap-allocated structs



Wolfgang Jeltsch <jeltsch@tu-cottbus.de> wrote in message
news:bhblvh$vocjc$1@ID-77306.news.uni-berlin.de...[color=blue]
> 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?[/color]

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



Closed Thread