C versus C++

Can you have operators or inlines in C? (I never learned C… just skipped to C++). Hehe… here’s vector stuff that I’m pretty sure is fairly impossible in C.

class vector2d
{
public:
float x, y;
virtual float Length( void ) const { return sqrt(xx + yy); }
};

class vector : public vector2d
{
public:
inline vector( void ) { }
inline vector( float X, float Y, float Z ) { x = X; y = Y; z = Z; }
inline vector operator +( const vector &v ) const { return vector( x+v.x, y+v.y, z+v.z ); }
float z;
float Length( void ) const { return sqrt( xx + yy + z*z ); }
};

You can have everything in C that you can have in C++ (apparently). I’ve heard early C++ compilers would actually compile C++ into C first, then into machine code. You can even do everything in assembly language that you can do in C++ (don’t try that at home). Try compiling your programs to output assembly (MSVC++6 help says use the /FA switch).

Although what I’ve said above is true, there is no way I could rewrite some of my C++ code into C - like you say operator() calls and so on. You can have the same effect as inlining using macros though - #defines. This is pretty much exactly the same effect as an inline function except it’s done by the preprocessor instead of by the compiler.

Yes you can do every C++ feature in C, and every C feature in assembler and every feature in assembler you can do by directly hacking machine code. The point is no one actually does it, beacause you loose the whole point of higher level languages, abstraction. With abstraction you generally lose some performance but gain a lot in high level design and flexibility.

I agree totally, Harsman, I was just saying it’s possible in response to 2’s question.

One more point 2: if you have Scott Meyers’ excellent book “Effective C++”, read item 33 on the dangers of inlining constructors & destructors.

Last code point while I’m on my bandwagon : do you know about member initialisation lists 2? I’d probably change your second vector constructor to:

// I might even make the parameters const float&
vector(float X, float Y, float Z) : vector2d(X, Y), z(Z) {}

It’s just a little bit more efficient and cleaner. Don’t take this the wrong way 2 - there’s nothing wrong with your code - but I’ve read a lot of good books on C++ and sometimes I think I’m too clever for my own good Point is, I wish I had more people to help me with my coding, so I try to offer what I know when I can. You can never stop learning

[This message has been edited by ffish (edited 05-09-2001).]

You people who are looking for a non-MFC GUI class library may find www.wxwindows.org very interesting. Cross-platform, works on most
compilers and even has a built-in OpenGL canvas

Now I’m not gonna echo anybody’s opinion, people, so bear with me.

In the discussion of C++ vs. C I have to say that C++ has some VERY nice features to offer for very little overhead(function overload, OOP,templates). Especially templates are useful, because when you want a vector of your personal class Met, all you need is “vector<Met> v;”.

As for MFC, I think they’re evil, and you should use GLUT, if you’re going for portability. Programming WINAPI has a serious performance advantage:By calling InvalidateRect() at the end of your drawing function, you can hack Winblows to pump a large part of the system resources into your program, something that GLUT doesn’t, to my knowledge. You’ll love it when your fps hits 200!

Where

GLUT does has “Game mode”. I got like 600 fps with that. Kilgard just neglected to mention it in his manual. Here’s a site that has some good info on it:
http://www.fatech.com/tech/opengl/glut/index.php3?gameglut

Originally posted by john:
Hmm. I don’t want to launch into the whole imperative / object orientated debate. However, I will say this: OOP isn’t a tricky feature which is nice to have but is frivilous in all other repsects. It ins’t like having a funky LCD display on your cd player that looks pretty but is ultimately useless. OOP is a methology of programming; It is, no less, an important prorgamming paradaigm.

This all depends on what you’re doing. For me, in my 3D engine, OOP is absolutely essential. It’s allowed me to write code that is very clean, organized, and easy to understand. It just makes sense - I’m creating a fake world in 3D so I have a world class. The world has different objects in it so I have various object classes. They load themselves, they move themselves if needed, they draw themselves, and they delete themselves when your done with them. They do everything. And if I need to have another world ready while another is already loaded, I can just create another instance of my world class. I can’t imagine writing anything without OOP anymore.

About Caml:

i don’t see anything interresting in this language.

Hehe, this is a popular topic. I would like to restate my opinion. You should use C, and when you are a REALLY good programmer, then move on to OOP. This is because OOP will only confuse you when you arent experienced enough. Some people might say it is easy, in some ways it is, but if you want to utilise it properly, it is quite complicated.

I sort of agree, but only insomuch as learning C is much easier than learning C++. There are plenty of other OO languages that are a hell of a lot easier to learn than C++. Java seems to be a popular one for undergrads these days. Python is very clean and very simple, though supposedly quite powerful. C++ though, is not a beginners language. I have been using it for 3 years and I’ve read some very good (and very difficult) texts on it. There are so many features and so many quirks that I feel I’ll never finish learning, despite having 10s of thousands of lines of code under my belt. If you’re really serious about learning C++, here’s a list of my favourite texts:
The C++ Programming Language by Bjarne himself - a must have (read it 3 times).
The ANSI/ISO C++ Professional Programmer’s Handbook by Danny Kalev (from the standards committee) (read it twice).
Effective C++ and More Effective C++, both by Scott Meyers (read 3 times or more).

I would say C++ is worthwhile learning. As the OO proponents say, OO is a natural way to conceptualise the world. It lends itself well to 3D graphics since we can all identify the objects used easily (vectors, vertices, triangles, etc through to houses, people and other composite objects). It also is so much more valuable to me than C ever was in terms of reuse of old classes. That alone was worth the investment in reading those books.

Well I’d go for Objective-C. The gcc compiler of about any *nix system should compile it. It’s an extension of ANSI C and it’s extremely easy to learn compared to C++. Knowing C, the rest should take less then 5 days to learn (I did it in less then one day). The plusses of Objective-C is that it contains no multiple inheritance and it has a descriptive method syntax, dynamic binding etc.

They stole some SmallTalk syntax when inventing the language unfortunaly not the garbage collection… oh well we can’t have everything!

CYA

The beauty of OOP/C++ is that you can easily develop and alter an application, adding features you didnt thought about before, while keeping your code clean and readable.
In C adding new stuff can make your code become unreadable.

Originally posted by Benjy:
It’s hardly an ‘ugly trick’ returning a pointer to an object rather than a copy of it. If anything, it seems the most logical way to do things, especially once you get into deep vs shallow copying.

By “logical” you must mean “efficient”.
“a=b+c” is logical, “sum(&b, &c, &a)” is efficient but not logical.

Just try implementing a vector-class supporting the arithmetic operators, using pointers instead of copying. Your vector calculations will be as ugly as in Java: “sum(mul(a, b, t1), mul(c, d, t2), e)” instead of “e=ab+cd”. What’s the point of operator overloading if it’s not efficient and noone wants to use it?

The point of OO is to be able to extend the typesystem and to be able to use all types in a generic way. Using Vector or any other class should be as straight forward as using int or float.

For me, this is the end of this discussion. Just read the classic “A Critique of C++” (http://burks.bton.ac.uk/burks/pcinfo/progdocs/cppcrit/index.htm) and maybe you’ll realize how many flaws there are in C++.

I’m not quite sure I understand you properly Mamba - I’m not a proper programmer, just a chemical engineer who likes computers.

Surely a logical and efficient way to implement operator overloading in, for example, vertex classes would be something like:

class vertex
{
public:
float x,y,z;
vertex* operator +(const vertex& rhs);
};

vertex* vertex: perator+ (const vertex & rhs)
{
vertex*temp=new vertex();
temp->x=x+rhs.x;
temp->y=y+rhs.y;
temp->z=z+rhs.z;
return (temp);
}

By returning a pointer to a vertex, you only have to create one new vertex object and don’t have to copy it at all.

I realise it’s a bit odd writing vert1=*vert2+*vert3, and that you can’t do vert1=*vert2+*vert3+vert4 without writing the nasty line vert1=(*vert1+*vert2)+*vert4, but at least it’s better than nothing

[This message has been edited by Benjy (edited 05-12-2001).]

Benjy, that code has a few problems. For starters, you shouldn’t allocate memory in a function like that unless you really know what you’re doing. What if you gave that code’s interface to another programmer and they wrote something like this:

vertex v1;
vertex v2;
for (int i(0); i<1000000; ++i) {
vertex *v3 = v1 + v2;
}

Where does the memory go that has been allocated by the new function inside operator+ ? It’s a memory leak which would be at least sizeof(3*float)*1000000 - 1 bytes. The way to write the functions you need is like this:

class vertex
{
public:
float x,y,z;
const vertex& operator+=(const vertex& rhs);
};

const vertex& vertex: perator+=(const vertex& rhs)
{
x+=rhs.x;
y+=rhs.y;
z+=rhs.z;
}

const vertex operator+(const vertex& lhs, const vertex& rhs)
{
vertex temp(lhs);
return temp+=rhs;
}

This code is efficient, easy to use and natural. You can call it as v3 = v1 + v2 or v2 += v1 without pointer dereferencing. The operator+= returns a const reference, like it should, which is for all purposes just a const pointer to a const object so you get pointer efficiency, plus you don’t have to create an object since it’s a member function. The operator+ pretty much has to create an object and return it by value, for reasons that I won’t go into here.

[This message has been edited by ffish (edited 05-13-2001).]

Thanks for the advice. I’ve been a casual programmer for years, but next academic year I start a phd that’s going to require a lot of coding, so I’m trying to learn as much as possible.