Trying to code pure valid C++

Started by
82 comments, last by programering 16 years, 3 months ago
I am trying to code my game in pure C++ style. But this is what gets me stuck; In C++ is the idea to use class'es. I want(ed) to use my CBitmaps class but it doesn't go because of the/my object definition. I always insist to have definitions as struct's, like this:

struct sObjectDef
{
	sBitmaps  *m_pBmps;	// dynamic array of bitmap structs.
	Uint8	   m_nBmps;	// the amount of bitmap structs of the above array.
	sSequence *m_pSeqs;	// dynamic array of sequence structs.
	Uint8	   m_nSeqs;	// the amount of sequence structs of the above array.
};

Structures with no functions, just data members. Therefore I can't use the CBitmaps class. Or if I make definition a class, like this:

class CObjectDef
{
public:
	CObjectDef(char *nameID);
	std::string m_strNameID;
	std::vector<CBitmaps *> m_vecBmps;
	std::vector<CSequence *> m_vecSeqs;
};

With vectors and then I can have it in class'es. So I don't know if I should have the object definitions as struct's or if everything in pure C++ shall be classes. When should one use struct's or classes? When should one use std::vector's or dynamic arrays? And one more thing should I have the constructor/function parameters char *pointer, std::string or std::string& reference. How is the most valid C++ coding manner way of doing it? It's things like this which gets me stuck and holds me from getting any results done, please help.
Advertisement
Quote:
When should one use struct's or classes?


struct and class are almost the same thing in C++. A struct can have member functions, constructors and destructors just like a class. The main difference is that in a struct everything is public by default and in a class everything is private. Other than that, structs default to public inheritance and classes default to private.

Some people prefer to use struct for small POD types. But its just personal preference.

Quote:
When should one use std::vector's or dynamic arrays?


I assume "dynamic arrays" refers to raw new [] and delete []. Always use std::vector.

Quote:
And one more thing should I have the constructor/function parameters char *pointer, std::string or std::string& reference. How is the most valid C++ coding manner way of doing it?


In C++ we use std::string to represent text. std::string is a complex object, so we prefer to pass it by const reference rather than by value. So const std::string &parameter.
Quote:
Original post by programering
When should one use structs or classes?


The only difference between classes and structs is that the default visibility for structs is public and for classes it's private. Both can have c'tors/d'tors, member functions, access specifiers, etc.

Quote:When should one use std::vector's or dynamic arrays?


In general, vectors are a better choice (and before you ask, no they are not slower that arrays).

Quote:should I have the constructor/function parameters char *pointer, std::string or std::string& reference.


Again, in general, std::string is the better choice. As far as parameter passing conventions, prefer passing by const reference whenever possible.

This site has explanations regarding the reasoning for the above arguments, as well as a lot of other useful information.
What do you think, shall I use struct or class as definition? What would be most valid the C++ standard?
Quote:Original post by programering
What do you think, shall I use struct or class as definition? What would be most valid the C++ standard?


Do you understand the differences as I have laid them out? They *are* the only differences. Its purely a style thing, since most people like to be explicit about public and private members anyway. If you just want an answer I would say use 'class', because privacy is a good thing.
Quote:Original post by rip-off
If you just want an answer I would say use 'class', because privacy is a good thing.

But the object class must have access to it's definitions. That's why I have the object definitions. You know the things about why you have definitions right? How would you have done it?
class CObjectDef{public:	CObjectDef(char *nameID);	std::string m_strNameID;	std::vector<CBitmaps *> m_vecBmps;	std::vector<CSequence *> m_vecSeqs;};class CObject{public:	CObject(int x, int y, Uint8 def);	void Update(void);	void Draw(short x, short y);private:	Uint8	  m_dir;	sPosition m_pos;	CBitmap *m_pBmp;};
Since you're interested in writing idiomatic C++, I'll point out a couple of things:
// This is a bit subjective, but I'd argue that there's no reason// to prefix your class names with 'C'.class CObjectDef{public:    // I'd make this argument a std::string (constant reference, of course),    // rather than a char*. That way you can submit either a char* or a    // string object.    CObjectDef(char *nameID);    // There's no need to encode the variable type in the name. For example,    // this should just be m_nameID, not m_strNameID.    std::string m_strNameID;    // That's especially true here, since you might choose to change the    // container type at a later date (e.g. to a list). These should really    // just be m_bmps and m_seqs (or better yet, m_bitmaps and m_sequences).    // Also, this is a good place to use typedefs, e.g.:    // typedef std::vector<CBitmaps *> bitmaps_t;    // bitmaps_t m_bitmaps;    // Finally, these containers should probably store smart pointers rather    // than raw pointers, but we won't go into that here.    std::vector<CBitmaps *> m_vecBmps;    std::vector<CSequence *> m_vecSeqs;};class CObject{public:    CObject(int x, int y, Uint8 def);    // In C++ we typically don't use 'void' to indicate an empty argument    // list; just leave the parentheses empty (e.g. void Update()).    void Update(void);    void Draw(short x, short y);private:    // Is Uint8 from SDL? For portability purposes I'd just use an int,    // or, if you really want a particular number of bits, use a typedef    // from the Boost 'stdint' header (can't remember exactly what it's    // called).    Uint8 m_dir;    sPosition m_pos;    CBitmap *m_pBmp;};
Now after all that, I have to admit that I'm not sure I understand the actual question that you're asking, so perhaps you could clarify it a bit.
Quote:Original post by programering
You know the things about why you have definitions right?

Class definitions? Yes.
Definitions in the sense that you seem to be talking about them? Not really - enlighten us.

It's not clear what you're trying to achieve here.
I think the problem is you're not really using terminology particularly well and it's somewhat confusing.

Quote:With vectors and then I can have it in class'es.

So I don't know if I should have the object definitions as struct's or if everything
in pure C++ shall be classes.

You can have vectors in structs, you can have methods too. You seem to think that structs are somehow not 'pure' C++? They're part of the language for sure.

Quote:When should one use struct's or classes?

It's purely stylistic. Most people are accustomed to reading class, however some people (like myself) may use structs for POD types.

Quote:When should one use std::vector's or dynamic arrays?

This is an example of poor terminology usage, a std::vector is practically always a dynamic array implementation. We assume that when you say 'dynamic array' you mean using new[] and delete[]. The answer is use std::vector always [smile]

Quote:And one more thing should I have the constructor/function parameters char *pointer, std::string or std::string& reference.

Prefere to pass strings by a constant reference, so that's: const std::string&

Quote:How is the most valid C++ coding manner way of doing it?

No such thing as 'most valid', they're all equally just as valid, some ways are just better practice.

If style is viciously impeding on getting things done then most programmers tend to opt for what works first and foremost; then they worry about how to make it better.
It sounds like you are striving for the One True Method of programming C++. pure? its "pure" if your not using any other languages inside the code. (no including cstdio, you must use std, and so on) Valid? if it compiles its valid. No warning allowed.

Thats it, really.
I just wanted to see if he would actually do it. Also, this test will rule out any problems with system services.
Quote:Original post by shotgunnutter
It sounds like you are striving for the One True Method of programming C++. pure? its "pure" if your not using any other languages inside the code. (no including cstdio, you must use std, and so on) Valid? if it compiles its valid. No warning allowed.

Thats it, really.
I'm interpreting 'pure' as meaning 'idiomatic' in the context of the OP's post. But, I could be wrong in that interpretation...

This topic is closed to new replies.

Advertisement