I recently got a pretty good grasp on the object-oriented side of C++, but I still don't know everything when it comes to templates and overloading. IIRC you can overload methods, but can you overload templates. Also, can somebody explain protected variables and dynamic vs. static memory. Also, is it good practice in C to use a struct based system for pseudo object-orientation? As you that know me can see, I am getting better =D
Not exactly. You can't do this: Code (Text): template<class T> class X { // ... }; template<class T, class U> class X { // ... }; However, you can specify alternate versions of a class for each possible template parameter. For example: Code (Text): template<class T> MyClass { T *t; public: void doSomething() { t++; } }; template<int> MyClass { int t; public: void doSomething() { t = 0; } }; (Sorry, I can't think of a good example tonight.) You can also make template arguments optional. For instance: Code (Text): namespace std { template<class T, class Alloc = allocator<T> > class vector { /* ... */ }; } might be the declaration for your compiler's std::vector class. You only need to worry about custom allocators if you need to do your own memory management, so most people ignore the Alloc argument and the standard library will load its own, which just does new and delete. Code (Text): class Base { int privateInt; protected: int protectedInt; public: int publicInt; }; class Derived : Base { public: void f(); }; void Derived::f() { this->privateInt = 0; // WRONG; private doesn't carry through this->protectedInt = 1; // OK; protected variables carry through this->publicInt = 2; // OK } void g(Derived &d) // global function { d.privateInt = 0; // WRONG; private used outside scope d.protectedInt = 1; // WRONG; outside the class, protected == private d.publicInt = 2; // OK } Does this make it clear? Code (Text): void f() { static int x = 0; // this variable is in static memory; it exists until the program quits and &x will always yield the same value in every function call int *y = new int; // this variable is in dynamic memory; it exists until it is deleted or the program quits; also y will point to a different memory address each time int z = 0; // this is also in dynamic memory and is created each time the function is called on the call stack (so &z is different each time) and is destroyed when the functio nreturns } You could; see gobject. However, people would rather you write in pure C++. On a side note, Ken Thompson's (the Unix co-creator) extensions to C in Plan 9 from Bell Labs let you do simple inheritance: Code (Text): struct Lock { /* multithreaded locks */ }; struct AsynchronousObject { int data; struct Lock; }; extern void lock(struct Lock *l); void f(struct AsynchronousObject *o) { lock(o); }
So...protected variables private globally, but are passed down through inheritance. As for memory, I kinda get it, does it have to do with how long the variable exists in the programs existance. As for the templating, I am not that far yet, I meant Code (Text): template <class myType> doThis(myType myVar) vs. template <class myType> doThis(myType myVar, myType myOtherVar)
Yes, and it also has to do with when the memory is created. Static = compile time, dynamic = run time. Oh, I thought you meant class templates. Function templates have more complicated rules. I suggest you check a C++ reference book for that.
You can have overloaded template functions, just as you can with regular functions. Code (Text): template <class T> T* create(float x = 0, float y = 0) { //function which auto-names created object } template <class T> T* create(std::string name, float x = 0, float y = 0) { //function which uses specified name }
Cool. Anybody got a good PDF that lists out all the parts of the standard C++ library in a style similar to javadoc or the back of the KnR book. Also, inheritance in C++, is it like extends in Java. Code (Text): class myClass extends yourClass vs. class myClass : yourClass
No, but there is http://www.cppreference.com/. Almost. C++ has rules for how protected and public work in inheritance that are not very useful for most purposes. You would just have to say Code (Text): class myClass : public yourClass The public keeps the same precedence rules as the parent class.
Okay, a few questions. What are the differences between, pros/cons, and significance of: Code (Text): int *ip; ip = (int *) malloc(sizeof (int)); *ip = 3; /* and */ int *ip; *ip =3; /* and */ int ip; ip = 3;
In the first one, ip points to memory allocated from the heap. You need to free() that memory before your program finishes or it will be leaked. However, you can use that memory from anywhere in your program as long as you maintain a pointer to it. The third one is allocated on the stack. As soon as you are out of your current scope (which usually means the end of the current function), the current "plate" is popped and that memory is returned to the system to be used later. Even if you managed to maintain a pointer to the exact memory address where ip is being allocated, it would not be safe to use it since the compiler has assumed you are no longer going to use it and will allow it to be used elsewhere. The second one is bad because the value of ip is unknown garbage, and then you dereference it. In other words, you've just written "3" to a random location in memory. The pros/cons between stack and heap allocation really depend on what you need to do. For one thing, depending on what kind of system you are working with (for example, an embedded system such as a handheld GPS), you need to be careful about allocating a large chunk of stack memory since you may overflow the stack, but if you only need your variable for the length of a short function, it'd be cumbersome to malloc/free it. Let me know if that gives you the information you want, or if there's anything I can clarify.
This may be obvious already, but I just want to add that just because it's a pointer, doesn't mean that it has to be on the heap. I often see people repeat the misconception that "pointer = heap", when it's not necessarily true, since pointers can point to either stack or heap memory. As evidenced by code like this: Code (Text): int num = 42; int *p = # 'p' in this case is a pointer to a stack variable (and should not be used if the variable goes out of scope).
That's true. Thanks for clarifying that. Pointers are *usually* used to allocate memory on the heap, but that doesn't have to be the case. The most helpful way I learned to think about it is: "pointer == memory address". You have to keep in mind what happens to that memory when your function and your program end.
I think I meant static on the second one. As for the others, I kinda get it, is mallocs purpose to make an absolute sized space for a variable instead of just randomly putting it somewhere?
You're asking two different questions. malloc(size) will find free memory of the given size and return its address. This can be any address at any time. This address will always point to locations that don't already hold data. You can look up implementations of malloc in several places to understand what it does.
Ohhhh, I think I get it now. *malloc(size_t x) just attempts, when possible, to get you a pointer to a guaranteed chunk of x bytes?
New question, this time regarding GTK+. I understand UI building and the rudiments of the signal/callback system, but I am stuck now. How do I access the return value of a callback. I have one where when I click a button, it returns the active text in a combo box, but I have no clue how to access the returned string. I'm an idiot, disregard that. A question though, how can I make a callback operate on multiple g_objects. Do I put them all in g_signal_connect or just make separate callbacks for all.
Surprisingly, I have no questions besides, what should I try to learn now? I have nailed the Standard C Library, and GTK+ is coming easy. Should I work more on graphics with GDK or OpenGL, or should I try some other types of libraries like audio libraries. I just dunno what direction is best to go. I'm not *THAT* interested in game development, but I can't think of a practical application that doesn't already exist.
Most audio libraries are piss easy to use. Maybe try learning to use something like wxWidgets? I dunno.