Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Irrational fear of C++

Name: Anonymous 2010-01-22 14:25

I have an irrational fear of C++. More than once have I swayed away from useful libraries because all they offered was a C++ API. I have perused several books on C++, but they have done nothing but strengthened the feeling that C++ is too complex to reasonably handle. What to do?

Name: Anonymous 2010-01-22 14:29

Read SICP.

Name: sage 2010-01-22 14:34

>>2
Thank you for your effort, but I've already read SICP.

Name: Anonymous 2010-01-22 14:34

The fear of C++ is perfectly rational

Name: Anonymous 2010-01-22 14:42

>>4
Pretty much this.

Name: Anonymous 2010-01-22 14:51

>>1
More than once have I swayed away from useful libraries because all they offered was a C++ API.

and you acted right these fucking nigger could have offered a nice and clean C interface, but no it has to be C++.

man i hate such assholes.

Name: Anonymous 2010-01-22 15:07

>>6
What's the matter? Too powerful for you?

Name: Anonymous 2010-01-22 15:09

>>7
AAAAHAHAHAHAHAHAAHAHAHAHAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAAHAHAHAHAHAHAHAHAHAHAHAAHA!

Name: Anonymous 2010-01-22 15:10

>>8
Don't break the horizontal hold.

Name: Anonymous 2010-01-22 15:11

I would hope you at least know C, which is reasonable given its intended use.

I only know a subset of C++ -- I'm barely familiar with the standard API (I am a Qt faggot), and I don't know how magical automagic pointers work.  I plan to learn C++ properly some day, but right now I just need it to get a particular job done.

Name: >>1 2010-01-22 15:14

>>10
I know C very well, it's C++ I have a problem with. Now that you mention it, Qt is one of the things that makes me feel like I should know something about C++.

Name: Anonymous 2010-01-22 16:08

You shouldn't be afraid of C++: it can't hurt you, even though it wants to. You just need to realize these three things.

1. You can get work done in any language, no matter how laughably bad it is.

2. You don't need to be an expert on C++ to do that. You just need to know it well enough that you aren't basically writing C. Most programs won't require template magic (which isn't really all that tricky in the simple case), and some of its "features" like operator overloading are best ignored completely and were just included to throw you off the trail (busy work).

3. You're just as capable of writing decent code, by C++ standards, as the next guy, modulo practice. Nobody gets off easy with C++. You'll perform up to standards.

It might help you to call it Sepples, too.

Name: Anonymous 2010-01-22 18:30

>>12
2. You don't need to be an expert on C++ to do that. You just need to know it well enough that you aren't basically writing C. Most programs won't require template magic (which isn't really all that tricky in the simple case), and some of its "features" like operator overloading are best ignored completely and were just included to throw you off the trail (busy work).
This is a lie. You need to know a shitload about C++, otherwise sooner or later you will get caught by a Gotcha!

Also, no, you can't just ignore the parts you don't like, because some C++ library will ALWAYS use it (STL alone uses 90% of the damn language).

The only part that isn't a lie is that you don't need to be an expert. That's true, because people actually do get work done in C++, even though it's not possible to be an expert at it.

Name: Anonymous 2010-01-22 18:33

>>13
So it's a lie, except it's true?

Name: Anonymous 2010-01-22 18:34

>>13
Buggy, memory-leaking, unoptimized, program-crashing work, sure.

Name: Anonymous 2010-01-22 18:54

>>10
and I don't know how magical automagic pointers work.  I plan to learn C++ properly some day
No time like the present! I'm feeling helpful today, so here's a hand crafted zero-overhead non-transferable scoped smart pointer, written thirty seconds ago, just for you anon. Paste this sucker into your void.h:

template <class T>
class scoped_ptr {
public:
  explicit scoped_ptr(T* t) : t(t) {}
  ~scoped_ptr() {delete t;}
  T* operator->() {return t;}
  T& operator*() {return *t;}
private:
  T* t;
  scoped_ptr(const scoped_ptr<T>& other);   // unimplemented; not allowed
  T& operator=(const scoped_ptr<T>& other); // unimplemented; not allowed
};


This is the basis of a smart pointer. It's just a container that you put a pointer into; when the container is destroyed, it deletes the pointer. The idea is that you stack-allocate or field-allocate the container, so it automatically frees the pointer when it goes out of scope. That way the object can't leak if an exception goes by, if you return in the middle of a function, etc.

However it overloads operator-> and operator*, so you can use it like a regular pointer. It also disallows copy and assignment, so it can't be transferred. So to use it, instead of Foo* foo(new Foo()), you write scoped_ptr<Foo> foo(new Foo()) (notice I've stack-allocated scoped_ptr, which internally has a foo pointer).

Here's some test code, try it out:

#include <cstdio>

struct tester {
  tester() {printf("constructor\n");}
  ~tester() {printf("destructor\n");}
  void hello() {printf("hello\n");}
  void world() {printf("world\n");}
};

int main(void) {
  scoped_ptr<tester> obj(new tester());
  obj->hello();
  (*obj).world();
}


More typically though, smart pointers are transferable in special ways. For example shared_ptr internally keeps a reference count, so it knows how many copies you have about and frees the object when the last shared_ptr is destroyed. There's overhead for this though. auto_ptr and unique_ptr are transferable, and only one of them holds the real reference (it moves instead of copies); this is how you return smart pointers from functions. These get very complicated in order to implicitly convert smart pointer types based on the inheritance tree of their contained pointer. And of course you need a special smart pointer for arrays, because you have to use delete[] instead of delete.

Name: Anonymous 2010-01-22 20:56

>>16

I have been enlightened by a sepples EXPERT

This tutorial is indeed superior to most anything else out there

Name: Anonymous 2010-01-22 21:06

>>16
*_*

Name: Anonymous 2010-01-22 21:32

>>16
So, when you overload operator*, what happens if you do *ptr1 * *ptr2, for example, assuming they're both scoped_ptr instances? Or *ptr * ptr, for example, even though it's probably rare that you would want to do that? How come that isn't a conflict? Would it fail as unimplemented in both cases?

Name: Anonymous 2010-01-22 21:33

>>19
in the second example, slight typo: assume the ptrs are two separate instances (like ptr1 and ptr2).

Name: Anonymous 2010-01-22 23:00

>>19
Because the signatures of those operations are different. One is a unary operator used on the object itself and one requires a right-hand side.

Overloading the multiplication operator: T operator *(const T &);
Overloading the unary dereference operator: T & operator *();

Name: Anonymous 2010-01-22 23:02

>>21
Probably should've returned a T & for the multiplication operator. Whatever, that's not the point of the example.

Name: Anonymous 2010-01-23 2:19

>>21
As you might notice from this example, it's entirely possible to overload operators with multiple operands. For example:

T & operator *(const T &);
T & operator *(int);
T & operator *(const U &);


This allows you to, for example, have a Product object that expresses multiplication. Then you can use primitive integers on it as though it was normal multiplication, as well as being able to use other Products. You wouldn't be able to achieve this effect any other way. You could also have an Exponent object, which would represent U and allow you to modify the Product accordingly.

This is what's meant by "exploit the intuitive properties of a class." The problem with this is that for any non-trivial function, it's rare that people agree on what the correct operator is. It's obvious enough for string and maybe vector, but after that, it gets a little hazy.

Actually, speaking of vector, that's a good way of expressing the operator overloading. You might want to add an element to a vector or you might want to add entire vectors:

vector<T> & operator +(const T &);
vector<T> & operator +(const vector<T> &);


If you do use operator overloading, be certain to document what the operators actually do, because what seems obvious to you might seem completely illogical to someone else: Like jumping off a building to learn how it feels to hit the ground.

Name: Anonymous 2010-01-24 22:30

EXPERT SEPPLES BAMP

Name: Anonymous 2010-11-15 2:57

Name: sage 2011-07-25 23:20

<code>
anus
  anus
    anus
</code>

<pre>
anus
  anus
    anus
</pre>

Name: Anonymous 2013-09-01 13:56


Apparently I went to Japan to take a class in "gun control." But the first few days of classes is all squirting each other with water pistols and even plastic syringes. During one of the classes I overhear classmates saying they want to go to the club after class. I deduce that the club is actually the arcade at the mall. The group needed one more person for some reason, and I was about to volunteer myself, but I notice one of my classmates on the other side of the room looks like a girl I knew in elementary school.

Name: Anonymous 2013-09-01 15:28


I have a problem with the game. So i DL the game the same time it released on previous Comiket. Patch it to 1.01, play a bit and stop playing it for a long time... Until recently

Don't change these.
Name: Email:
Entire Thread Thread List