|
shrughes posted:How would you destroy the objects you've constructed in the region? Write a macro that initializes a wrapper class instance with the alloca'd memory(since you can't alloca in the constructor of the class since the memory is deallocated when current function returns). Then just to trigger placement new/delete as needed on the memory buffer, preferably in the class constructor/destructor so you get RAII behaviour.
|
# ? Mar 30, 2014 11:56 |
|
|
# ? Jun 1, 2024 20:11 |
|
I loled at this bug report: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19131
|
# ? Mar 30, 2014 12:04 |
|
I have a beginner question that I can't figure out. It has to do with either inheritance or vectors I guess? I have two classes like this: code:
It works like I want it to when I do it like this: code:
code:
I tried code:
code:
code:
|
# ? Mar 30, 2014 12:09 |
|
Zaito posted:I can obvious make a vector<Child> and have it work, but since I intend to have different flavors of Parent, I want an array which can store all of them. Is there a way to do this or do I have to find a workaround? You can't put a Child in a vector<Parent> - when you try, all the Child parts of the object are cut off and it becomes a Parent. What you can do, is put a pointer to a Child into a vector<Parent*>. That will then do what you want.
|
# ? Mar 30, 2014 12:20 |
|
Qwertycoatl posted:You can't put a Child in a vector<Parent> - when you try, all the Child parts of the object are cut off and it becomes a Parent. To slightly expand on what's going on here, vector<Parent> stores objects "by value". It'll allocates precisely the memory space required to store all the stuff that goes into a Parent at each position and no more, it has no idea how much space a Child takes and cannot store one. When you then do vec.push_back( Child() ); you're not storing the Child you just constructed directly but actually trying to do a copy insertion of it into the vector. The vector is taking the Child() and passing it to the (implicit but still present) copy-constructor Parent(const Parent &other); when it's constructing the Parent that it stores. This is a perfectly valid parameter -- a Child is a Parent and it can be passed to the copy-constructor, creating a Parent in that spot in the vector.
|
# ? Mar 30, 2014 12:35 |
|
Also, if you can use C++11 and you want to use pointers in a vector and not have to manage the memory yourself somewhere else, you can use something like std::vector<std::unique_ptr<Parent>>.
|
# ? Mar 30, 2014 13:30 |
|
Qwertycoatl posted:You can't put a Child in a vector<Parent> - when you try, all the Child parts of the object are cut off and it becomes a Parent. This does indeed do what I want, thanks!
|
# ? Mar 30, 2014 14:49 |
|
shrughes posted:I loled at this bug report: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19131 heh
|
# ? Mar 31, 2014 04:04 |
|
I'm trying to build a constructor and when I set a boolean variable equal to another boolean variable Xcode gives me error "Expected Expressions"C++ code:
|
# ? Mar 31, 2014 05:43 |
|
auto is a C/C++ keyword
|
# ? Mar 31, 2014 05:49 |
|
pseudorandom name posted:auto is a C/C++ keyword Well that will do it, cheers.
|
# ? Mar 31, 2014 05:52 |
|
I'm back with another hopefully obvious yet stupid question: I'm supposed to Use inheritance to derive two new classes Car and Truck from the base class Vehicle, which is available behind (Vehicle.h). I'm getting the error "C++ constructor must explicitly initialize the base class which does not have a default constructor" for the following code:C++ code:
C++ code:
C++ code:
I have really been struggling with C++ because it's so wildly different from Java, and Inheritance at one point was similar to Java but now it's just not making any sense to me
|
# ? Mar 31, 2014 21:18 |
|
Because Vehicle has a defined constructor with arguments and no constructor without (non-default) arguments, you need to explicitly call one of its constructors before any derived class's constructor runs. The syntax for doing that isC++ code:
Now my question for you: why do your derived classes keep additional track of information that's already stored in the base class? e: Vehicle's destructor isn't virtual. Tsk, tsk, instructor. raminasi fucked around with this message at 21:34 on Mar 31, 2014 |
# ? Mar 31, 2014 21:31 |
|
He should be using initialization lists instead of assignment inside of the constructors anyway, but then there are a ton of "best practices" issues with that code so I probably shouldn't open a can of worms of nitpicking.
|
# ? Mar 31, 2014 21:35 |
|
GrumpyDoctor posted:Because Vehicle has a defined constructor with arguments and no constructor without (non-default) arguments, you need to explicitly call one of its constructors before any derived class's constructor runs. The syntax for doing that is Star War Sex Parrot posted:He should be using initialization lists instead of assignment inside of the constructors anyway, but then there are a ton of "best practices" issues with that code so I probably shouldn't open a can of worms of nitpicking.
|
# ? Mar 31, 2014 21:40 |
|
Tusen Takk posted:do you mean mfgName = mfg and dealerCost = cost? Well, yeah, but not just the assignment itself. I should probably take a step back and ask what exactly your derived classes are supposed to do, because there's almost assuredly a better way. (And if not, your instructor is a complete idiot.)
|
# ? Mar 31, 2014 21:46 |
|
GrumpyDoctor posted:Well, yeah, but not just the assignment itself. I should probably take a step back and ask what exactly your derived classes are supposed to do, because there's almost assuredly a better way. (And if not, your instructor is a complete idiot.) He definitely is an idiot, half of his code in the lecture slides is completely hosed up or lacking tiny little things like quotation marks around Vehicles.h in #include "Vehicles.h" so I spent half an hour trying to figure out what was going on with that. The classes are supposed to quote:The Car class adds two private data members, a string that contains a model name (like "Camaro") and a bool that is true if the car has a sunroof. Car should also have an appropriate constructor, and a member function showVehicle that displays its manufacturer, retail price, model name and whether or not it has a sunroof. I don't understand why you wouldn't just write one class without inheritance to do all of those things but I guess the point of the exercise is less about making sense and more about showing you how inheritance is supposed to work.
|
# ? Mar 31, 2014 21:51 |
|
Tusen Takk posted:do you mean mfgName = mfg and dealerCost = cost? edit: Also I can tell you right now that this instructor sucks. It looks like he's teaching you bad C++98 with no regard for more recent standards or best practices. Star War Sex Parrot fucked around with this message at 21:53 on Mar 31, 2014 |
# ? Mar 31, 2014 21:51 |
|
Star War Sex Parrot posted:I'm pretty sure he means: why are you declaring duplicate member variables in your derived classes (mfgName, dealerCost) when they already exist in the base class? If you're going to declare the same members all over again, it defeats one of the biggest reasons for inheritance -- the other being polymorphism. He basically is, when I used the latest C++ stuff in my code not only would it not compile on the server but he refused to accept it if it didn't work on the server. I should have never taken this elective as for your question: l have no idea, I was kind of just plodding along hoping to get it to work so that I can submit it then worry about other homework
|
# ? Mar 31, 2014 22:12 |
|
Tusen Takk posted:He definitely is an idiot, half of his code in the lecture slides is completely hosed up or lacking tiny little things like quotation marks around Vehicles.h in #include "Vehicles.h" so I spent half an hour trying to figure out what was going on with that. Yeah. SWSP was right about my question, but what I hadn't noticed at the time was that those fields on Vehicle are private, so your derived classes have to re-store them. Which is really stupid. Keep your head down, turn in poo poo that barely works, and don't think for a second that you're learning any kind of proper C++ here.
|
# ? Mar 31, 2014 22:29 |
|
GrumpyDoctor posted:Yeah. SWSP was right about my question, but what I hadn't noticed at the time was that those fields on Vehicle are private, so your derived classes have to re-store them. Which is really stupid. I assume that they're private by intent and the students should implement Car::showVehicle by calling Vehicle::showVehicle rather than reimplementing its functionality in Car, in order to demonstrate how to encapsulate common functionality in the base class and all that jazz. As the exercise is written it can be done without re-storing anything in the derived classes. It's not a fantastically chosen example of when to do this and not having a virtual destructor is pretty much inexcusable when teaching inheritance in C++ but the idea itself is not a horror.
|
# ? Mar 31, 2014 22:58 |
|
Sweet I got it working, thanks guys!
|
# ? Mar 31, 2014 23:42 |
|
To give the benefit of the doubt to the professor, the example code as it is shown here doesn't require a virtual destructor to exist. And he may not have gotten to the part of explaining them or polymorphism yet, hence the lack of a virtual.
|
# ? Apr 1, 2014 03:24 |
|
If the C++-based curriculum introduces inheritance before even mentioning polymorphism it is really bad curriculum. (Honestly, I think that using C++ to teach anything except C++ is a bad idea, but this is definitely not how you make the most of it.)
|
# ? Apr 1, 2014 03:39 |
|
GrumpyDoctor posted:If the C++-based curriculum introduces inheritance before even mentioning polymorphism it is really bad curriculum. (Honestly, I think that using C++ to teach anything except C++ is a bad idea, but this is definitely not how you make the most of it.) You have to teach what inheritance and encapsulation are before you can teach polymorphism. Every single book on C++ I have ever read introduces these concepts in that order.
|
# ? Apr 1, 2014 03:43 |
|
I'm pretty familiar with C but just now starting a project with C++. I want to do some simple 2d graphics in Windows, and must of what I see suggests that GDI+ is the best API for doing this. Is that the way to go or would I be better off looking into DirectX? All I want to do is draw simple vector graphics like lines and circles, and maybe a .bmp or two eventually. My main focus is finding something easy to get started in so simpler is better.
|
# ? Apr 1, 2014 04:47 |
|
OctaviusBeaver posted:I'm pretty familiar with C but just now starting a project with C++. I want to do some simple 2d graphics in Windows, and must of what I see suggests that GDI+ is the best API for doing this. Is that the way to go or would I be better off looking into DirectX? All I want to do is draw simple vector graphics like lines and circles, and maybe a .bmp or two eventually. My main focus is finding something easy to get started in so simpler is better. You can use GDI+ but its pretty ancient these days. If you are on a Vista or higher I would recommend Direct2D.
|
# ? Apr 1, 2014 05:20 |
|
There are a whole bunch of different libraries you could use for that. Personally I'd go with Qt, but that's because it's familiar to me. SFML is a nice simple interface for 2d graphics as well. You could also look at SDL or glut. They all provide cross platform window management/initialization boilerplate which is nice unless you really want to learn raw win32 calls. I find QML to be fairly easy to get started with, but actually integrating it with a complex c++ program is a bit of a challenge at first.
|
# ? Apr 1, 2014 05:30 |
|
xgalaxy posted:You have to teach what inheritance and encapsulation are before you can teach polymorphism. Every single book on C++ I have ever read introduces these concepts in that order. But how on earth do you effectively motivate inheritance without polymorphism? Every attempt I've seen has resulted in these idiotic "Car extends Vehicle" examples that have absolutely no resemblance to the problems you'd have to solve in a real application or the ways in which you would solve them.
|
# ? Apr 1, 2014 18:57 |
|
GrumpyDoctor posted:But how on earth do you effectively motivate inheritance without polymorphism? Every attempt I've seen has resulted in these idiotic "Car extends Vehicle" examples that have absolutely no resemblance to the problems you'd have to solve in a real application or the ways in which you would solve them. trying to pick up C++ "again", the last time I seriously read a C++ book was this one more than 20 years ago so needless to say a lot has changed though I have done some ad hoc hacking, ie not understanding wtf I was doing, since then.
|
# ? Apr 2, 2014 00:57 |
|
nebby posted:Eh, you can motivate inheritance just as a code reuse mechanism before introducing polymorphism. It's a bit contrived but for teaching purposes it works. This is the approach taken by Accelerated C++ iirc which I just finished working through. [public] inheritance for code reuse is both a coding and curricular horror.
|
# ? Apr 2, 2014 02:02 |
|
GrumpyDoctor posted:[public] inheritance for code reuse is both a coding and curricular horror. edit: re: public vs private (ie interface vs implementation motivated) the distinction seems irrelevant when first introducing someone to the concept of inheritance with the goal literally being to provide enough foundational knowledge to explain polymorphism. the alternative is you teach them template based polymorphism before virtual functions (or choose another programming language) -- good luck bootstrapping that one! nebby fucked around with this message at 03:08 on Apr 2, 2014 |
# ? Apr 2, 2014 02:48 |
|
nebby posted:Anytime you have a base class with a non-virtual function, or a non-pure virtual one at that, aren't you leveraging inheritance for code reuse? Not when the method is called using the base class type. Then it's the same as if you have a function on the outside of the class, using it (except for visibility). I don't think purity has anything to do with this -- you mean constness or functional purity, right?
|
# ? Apr 2, 2014 05:25 |
|
shrughes posted:I don't think purity has anything to do with this -- you mean constness or functional purity, right? "Pure virtual" functions in C++ are another name for what are called "abstract" functions in other languages - they provide a declaration of what the function is, but no definition (requiring it to be implemented by subclasses).
|
# ? Apr 2, 2014 06:10 |
|
Yeah, if I have a non-pure virtual function on a base class, I am providing a default implementation for a virtual function. Ie, this is letting me re-use that code in subclasses that do not need to override the virtual. Anyway the point is just that I don't see any way to teach polymorphism in C++ without an intentionally simplified exposition on inheritance as a preface, unless you want to teach templates first or think overloading is a sufficient demonstration (it's probably not.) If you want to avoid OOP and teach polymorphism then you probably need to use a language with alternative approaches to dynamic dispatch like multimethods. Julia is a cool one. nebby fucked around with this message at 06:29 on Apr 2, 2014 |
# ? Apr 2, 2014 06:25 |
|
I don't think a non-pure virtual function, or any other function in a base class, is any more an example of code reuse than any other function is. The non-pure virtual function is the same as having a pure virtual function with a "helper" providing a default implementation for any implementers that would want to use it. So it saves you from having to write some vacuous callers to the helper function. I don't think that's code reuse, it's more like a distinct notion, boilerplate elimination.
|
# ? Apr 2, 2014 09:01 |
I'm trying to learn C and I'm struggling with pointers and strings (duh). I'm trying to convert a pointer address into a long unsigned int just for curiosity's sake, but it's not working at all. I figure the issue is with getting actually getting the dereferenced pointer to be used as a string. I can't do: code:
code:
What's my fundamental failure here?
|
|
# ? Apr 2, 2014 12:09 |
Sulla-Marius 88 posted:What's my fundamental failure here? Part of it seems to be a misunderstanding between the relationships between pointers, integers and strings. A string in C is a pointer to some memory that contains a series of characters, ending with a "nul" character. Note that the "nul" character is not the same as a NULL pointer, it's just a character with a special value that gets treated specially. (A "sentinel" value.) A pointer, on the machine level, is an integer value, but if you depend on treating pointers as integers in regular software, chance is you're on the way to shoot yourself in the foot. An integer is a number value. A single character is also a number value, but the computer system happens to have a way to produce a printed character based on looking up characters' number values in a table. A string of characters can happen to contain only characters whose values are decided to represent digits in human language, so when you print that string you see it as a number. That doesn't make the string a number, it's still just a string. However a function such as strtol() will attempt to parse a string that contains a number and produce an integer value from it. That value is from the sequence of characters stored at the location the string is a pointer to, not from the value of the string pointer or something else. Now, if you wanted to take a pointer value, treat it as a numeric integer value, and produce a string that contains a human-readable representation of that integer value, you will have to do two things: First, typecast the pointer value to an integer value. (This is on the machine level a no-op, nothing actually gets converted, the typecast here only tells the C compiler that you want to treat the value as a different type, but the distinction typically doesn't exist for the CPU.) Now that you have an integer value, you can then use a function such as sprintf() to produce a string representation from the integer. Of course that requires you to have some a writable string (character array) for the function to write into. C++ code:
|
|
# ? Apr 2, 2014 12:45 |
|
Also if you want a signed or unsigned integer which is guaranteed to be big enough to hold a pointer, you can use intpr_t and uintpr_t respectively.
|
# ? Apr 2, 2014 13:00 |
|
|
# ? Jun 1, 2024 20:11 |
Thank you so much. I can't imagine why I'd need to use the pointer address as an integer, I was just struggling because I was trying to do it as a curiosity, it wasn't working, and I couldn't figure out why. It conflicted with my knowledge of the pointer value and of numerical conversion. It turned out, as per your code, that the final step I was missing was the explicit cast to treat the pointer as an unsigned long int when grabbing its value rather than pushing the pointer itself into an existing unsigned long int... if that makes sense. So this: code:
code:
For clarity, I didn't want the pointer address as an integer for any particular reason, I just wanted to iterate through a string and see how the addresses in memory increased as well, but it just wasn't working and I couldn't figure out why.
|
|
# ? Apr 2, 2014 13:24 |