struct in C++

G

Garma

In VC++ compiler

struct what
{
int a;
int b;
};

In C, an struct of type "what" is created like: struct what this;
In C++, an object of type "what" can be instantiated like this: what this;
Is this defined in C++ Standard or just VC++ compiler I am using
implemented?
 
A

Andrey Tarasevich

Garma said:
In VC++ compiler

struct what
{
int a;
int b;
};

In C, an struct of type "what" is created like: struct what this;
In C++, an object of type "what" can be instantiated like this: what this;
Is this defined in C++ Standard or just VC++ compiler I am using
implemented?

Yes, it is defined in C++ standard. In C++ in this particular context
(declaration) you can do it either way. Keep in mind though that in C++
'this' is a keyword. You can't create an object named 'this'.
 
E

E. Robert Tisdale

Garma said:
In VC++ compiler

struct what {
int a;
int b;
};

In C, an [object] of type "struct what" is created like this:
struct what this;
In C++, an object of type "what" can be instantiated like this:

what this;

Is this defined in C++ Standard?

No.
'this' is a C++ keyword.
Or just VC++ compiler I am using implemented?

The ANSI/ISO C standards define the C programming language.
The ANSI/ISO C++ standards define the C++ programming language

If you define:

typedef struct what {
int a;
int b;
} what;

in C, you can create an object of type "struct what" like this:

what w;
 
N

Nick Hounsome

Garma said:
In VC++ compiler

struct what
{
int a;
int b;
};

In C, an struct of type "what" is created like: struct what this;
In C++, an object of type "what" can be instantiated like this: what this;

No it can't - 'this' is a keyword.
Is this defined in C++ Standard or just VC++ compiler I am using
implemented?

Standard - but you can write:
struct what a;
In both C and C++ and it is still needed in some places in C++.
You can also get more C++ like in C by using:
typedef struct
{
int a;
int b;
} what;
what a;
 
H

Howard

E. Robert Tisdale said:
typedef struct what {
int a;
int b;
} what;

in C, you can create an object of type "struct what" like this:

what w;

You've got an extra "what" there, before the {, don't you? Should be just:

typedef struct {
int a;
int b;
} what;
....
what w;

-Howard
 
J

jeffc

Garma said:
In VC++ compiler

struct what
{
int a;
int b;
};

In C, an struct of type "what" is created like: struct what this;
In C++, an object of type "what" can be instantiated like this: what this;
Is this defined in C++ Standard or just VC++ compiler I am using
implemented?

Standard - new "feature" of C++ (over C).
 
E

E. Robert Tisdale

Howard said:
E. Robert Tisdale wrote:

If you define:



You've got an extra "what" there, before the {, don't you? Should be just:

typedef struct {
int a;
int b;
} what;
...
what w;

typedef struct what what;

typedef struct other {
what* p;
// . . .
};

struct what {
int a;
int b;
};

what w;
 
J

Jeff Schwab

Howard said:
You've got an extra "what" there, before the {, don't you? Should be just:

typedef struct {
int a;
int b;
} what;
...
what w;

-Howard

The first "what" is the struct's tag. The second is an alternative name
(via typedef) for the type "struct what".
 

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
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top