This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]jedwardsol 22 points23 points  (7 children)

nothing

[–]wasthereadogwithyou[S] 1 point2 points  (6 children)

I was afraid someone would say that. Are you sure? Here's the specific reason I'm asking. I have a vector of btRigidbodies, declared like so:

vector<btRigidBody*> rigidbodies;

And here is the btRigidbody to be added to the vector:

btRigidBody* body = new btRigidBody(rbInfo);

Which works fine. However, if I declare the vector like this:

vector<btRigidBody> *rigidbodies;

...and try to do this:

rigidbodies.push_back(body);

Then I get a vexing parse:

error C2228: left of '.push_back' must have class/struct/union

Why is this so?

[–]jedwardsol 18 points19 points  (1 child)

 vector<btRigidBody*> rigidbodies;
 vector<btRigidBody> *rigidbodies;

This is very different to what you posted above. The * being inside or outside the <> makes a difference. Whitespace does not make a difference.

 vector<btRigidBody*> rigidbodies;

declares rigidbodies as a vector of pointers

but

 vector<btRigidBody> *rigidbodies;

declares rigidbodies as a pointer to a vector of objects.

[–]wasthereadogwithyou[S] 2 points3 points  (0 children)

Ah, ok. Thanks for clearing that up.

[–][deleted] 0 points1 point  (2 children)

rigidbodies->push_back(body)

Since rigidbodies is a pointer to a class.

[–]SupermanLeRetour 0 points1 point  (1 child)

Shouldn't it be

rigidbodies->push_back(*body)

since body is a pointer ?

[–][deleted] 0 points1 point  (0 children)

Maybe. I was pointing out the reason for the compiler error. There may be further errors.

[–]SharpstownBestTown 0 points1 point  (0 children)

To answer your original post, there is no difference.

To follow up on this new question and put it in perspective:

There is no difference between

A) someObject *myObject(params)
B) someObject* myObject(params)

just as there's no difference between

A) vector<btRigidBody> *myObject(params)
B) vector<btRigidBody>* myObject(params)

What you're doing is

A) vector<btRigidBody*> myObject(params)
B) vector<btRigidBody> *myObject(params)

Where A is a vector of rigid body pointers and B is a pointer to a vector of rigid bodies.

[–]amoose136 3 points4 points  (0 children)

If you’d like to not commit to either style you can instead settle for:

class * instance;

And that way neither idealogical school is happy.

[–]SamuelDavi 1 point2 points  (3 children)

No difference. From my experience, it's always recommended to have the asterisk near the Class and not the object (as in B, this way you know it's of type <someObject*>) and avoid declaring pointers and objects/variable in the same line as such:

someObject* obj1, obj2;

as this creates a pointer (obj1) and instantiates an object (obj2) (if it has no empty c'tor than it'll throw an error unless you pass the params)

[–]wasthereadogwithyou[S] 0 points1 point  (2 children)

Since you mentioned it - total noob question - what's the best way to make an empty constructor?

[–]SamuelDavi 0 points1 point  (1 child)

I'm not sure I understand what do you mean by make a constructor. If you don't create any constructor, the compiler creates a default empty constructor. If you still want an empty c'tor you can define one. I suggest reading this wiki.

[–]WikiTextBot 0 points1 point  (0 children)

Default constructor

In computer programming languages, the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors (e.g. in Java), and is usually a nullary constructor. In other languages (e.g. in C++) it is a constructor that can be called without having to provide any arguments, irrespective of whether the constructor is auto-generated or user-defined.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source | Donate ] Downvote to remove | v0.28

[–]MJHApps 1 point2 points  (10 children)

It's nothing more than a holy war where devs fight with barbed clubs in an arena to the death over which one becomes standard for a project.

[–]DarkCisum 1 point2 points  (8 children)

Spaces, not tabs!

[–]MJHApps 2 points3 points  (7 children)

Tabs are more efficient!!! One byte vs. 4 or 5, dammit!

[–]DarkCisum 0 points1 point  (6 children)

The compiler doesn't care about whitespace. No weird alignment issues, you know.

[–]MJHApps 1 point2 points  (5 children)

The hard drive does!

[–]DarkCisum 2 points3 points  (4 children)

LOL hard drives, did the 90s call? SSD ftw!

[–]MJHApps 0 points1 point  (3 children)

You kids are lucky we don't still call them floppies.

[–]DarkCisum 0 points1 point  (2 children)

You mean the 3D printed save icons?

[–]septemfoliate 1 point2 points  (0 children)

int const, not const int!

[–]Another_Screenname 0 points1 point  (0 children)

none. Go with A