struct problem

J

Jon Bell

would someone please be so kind as to explain:
struct Vertex
{
Vertex(){}
Vertex(float x, float y, float z,
float nx, float ny, float nz, float u, float v)
{
_x = x; _y = y; _z = z;
_nx = nx; _ny = ny; _nz = nz;
_u = u; _v = v;
}

float _x, _y, _z, _nx, _ny, _nz, _u, _v;

static const DWORD FVF;
}; [snip]
is vertex(){} a function?

It's a constructor that takes no arguments/parameters, and does nothing.
It allows you to declare a variable of type Vertex without specifying
values for the data:

Vertex thisVertex;

in which case all the data members are default-initialized. In this case,
the data members are all floats, so they are not initialized at all and
contain whatever bit-patterns happened to be in those memory locations.

If you define no constructors at all, the compiler generates such a
default constructor for you automatically. However, if you define any
constructors of your own (as is the case here), you must define the
default constructor, too, if you want one.
 
D

Denver Dash

Vertex(){} is the default constructor. It is called every time an object of
Vertex is declared with no default parameters.

e.g.,
Vertex v1; \\ Vertex() is called
Vertex v2(0,0,0,0,0,0,0,0); \\ the second Vertex constructor is called.

Denver.
 
J

Jon Bell

hmmm ok so vertex() is understandably a constructor. What, then, is the
purposes of the braces afterwards?

That's the body of the constructor, which is empty, because it does
nothing.
Also, what exactly is this:

{
_x = x; _y = y; _z = z;
_nx = nx; _ny = ny; _nz = nz;
_u = u; _v = v;
}

That's the body of *that* constructor, which is not empty.

You can define the bodies of constructors and member functions either in
the class/struct definition, or separately. The following two versions of
your code accomplish the same thing:

//--- version A (your original version)

struct Vertex
{
Vertex(){}
Vertex(float x, float y, float z,
float nx, float ny, float nz, float u, float v)
{
_x = x; _y = y; _z = z;
_nx = nx; _ny = ny; _nz = nz;
_u = u; _v = v;
}

float _x, _y, _z, _nx, _ny, _nz, _u, _v;

static const DWORD FVF;
};

//--- version B ---------------------------------------------------

struct Vertex
{
Vertex();
Vertex(float x, float y, float z,
float nx, float ny, float nz, float u, float v);
float _x, _y, _z, _nx, _ny, _nz, _u, _v;
static const DWORD FVF;
};

// define the bodies of the constructors separately for version B

Vertex::Vertex()
{
}

Vertex::Vertex (float x, float y, float z,
float nx, float ny, float nz, float u, float v)
{
_x = x; _y = y; _z = z;
_nx = nx; _ny = ny; _nz = nz;
_u = u; _v = v;
}
 
C

Chris Theis

Dave said:
Wow! I never knew you could do that. Defining a constructor in the
definition? incredible.
thanks so much for your kind help.

dave :)

I guess it would be worth checking out any decent book like "Accelerated C++
by A. Koenig or Thinking C++ by B. Eckel for thinkg like this. Just keep in
mind that there are some subtleties for inlining constructors (covered in
Effective C++ Item 33 by Scott Meyers), which you might look up (but for the
moment this could be a little too advanced.)

Regards
Chris
 
M

muser

Denver Dash said:
Vertex(){} is the default constructor. It is called every time an object of
Vertex is declared with no default parameters.

e.g.,
Vertex v1; \\ Vertex() is called
Vertex v2(0,0,0,0,0,0,0,0); \\ the second Vertex constructor is called.

Denver.

chris i can't help you, but i take objection to Dave's apparent abrupt
reply which wasn't helpful in the slightest. To my mind Vertex is a
memeber function of type Vertex. I think chris was using a function
prototype and then the function definition. To my mind that seems the
most logical reading of the code.
Why can't some of you nerds just be pleasant for once, manners doesn't
cost a thing.
 
D

Dave

would someone please be so kind as to explain:
struct Vertex
{
Vertex(){}
Vertex(float x, float y, float z,
float nx, float ny, float nz, float u, float v)
{
_x = x; _y = y; _z = z;
_nx = nx; _ny = ny; _nz = nz;
_u = u; _v = v;
}

float _x, _y, _z, _nx, _ny, _nz, _u, _v;

static const DWORD FVF;
};

what on earth is this:?!?!?!

Vertex(){}
Vertex(float x, float y, float z,
float nx, float ny, float nz, float u, float v)
{
_x = x; _y = y; _z = z;
_nx = nx; _ny = ny; _nz = nz;
_u = u; _v = v;
}

is vertex(){} a function? a variable. Could someone at least give me a name
as to what's going on here so i know what to look for?

cheers
dave
 
V

Victor Bazarov

muser said:
"Denver Dash" <[email protected]> wrote in message

chris i can't help you,

Who's "chris"? This conversation is between "Dave" and "Denver".
but i take objection to Dave's apparent abrupt
reply which wasn't helpful in the slightest.

I think you need to check in your objections and get the names
straight. Denver's reply is right on the money. Vertex() is
_in_fact_ the default constructor. Dave didn't give any reply,
so it can't really be abrupt or unhelpful.

And if you can't help, how do you know the other reply wasn't
helpful? You apparently have no idea what to say here.
To my mind Vertex is a
memeber function of type Vertex.

Well, a constructor is a member function, yes. What's your
point?
I think chris was using a function
prototype and then the function definition. To my mind that seems the
most logical reading of the code.
Why can't some of you nerds just be pleasant for once, manners doesn't
cost a thing.

So, they are _nerds_ all of a sudden? Why can't you jocks learn
not to call people names?

All in all, you probably ought to read the whole thread before even
thinking of replying. Your post seems rather out of place...
 
I

Ivan Vecerina

Dave said:
Wow! I never knew you could do that. Defining a constructor in the
definition? incredible.

Note that the implementation of all member functions can be included
within a class definition (not only constructors). These functions
are then *implicitly* declared as 'inline'.

Because of this, and for readability purposes, only very simple
functions should be inlined within the class definition.


hth -Ivan
 
D

Dave

hmmm ok so vertex() is understandably a constructor. What, then, is the
purposes of the braces afterwards?
Also, what exactly is this:

{
_x = x; _y = y; _z = z;
_nx = nx; _ny = ny; _nz = nz;
_u = u; _v = v;
}
As far as I know, you can't declare variables in a class or struct. This
seems to be a definition rather than a declaration.
What is it actually doing?

cheers
dave
 
D

Dave

Wow! I never knew you could do that. Defining a constructor in the
definition? incredible.
thanks so much for your kind help.

dave :)
 
D

Dave

I have the ebook Thinking In C++ 2nd Edition, and it's brilliant. The author
presents a whole new perspective of OOP i've never seen before. Beautifully
written, but still no substitute for a wise old c++ mentor. maybe one day ;)

thanks for all the help
dave
 
C

Chris Theis

Victor Bazarov said:
Who's "chris"? This conversation is between "Dave" and "Denver".

As I'm the only Chris who has replied to this thread (at least I think so)
he must be talking to me. I guess it's too bad that he can't help me though
I don't recall asking him for help.

[SNIP][SNIP]

Somehow the use of "nerds" and "manners" in one sentence seems funny to
me...

Chris
 
J

jeffc

Dave said:
Wow! I never knew you could do that. Defining a constructor in the
definition? incredible.

In this sense a constructor is simply a function, and you can define any
define to be "inline" by simply defining it right at the point of
declaration in the class definition.
 
J

jeffc

muser said:
chris i can't help you, but i take objection to Dave's apparent abrupt
reply which wasn't helpful in the slightest. To my mind Vertex is a
memeber function of type Vertex. I think chris was using a function
prototype and then the function definition. To my mind that seems the
most logical reading of the code.
Why can't some of you nerds just be pleasant for once, manners doesn't
cost a thing.

You are amazingly confused.
 
D

Dave

hmmm despite all the reading and research i've done, i never knew that! i
felt so stupid, i couldn't for the life of me figure out what was going on!

your help is most appreciated
cheers
dave
 

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
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top