|
I'm doing a problem that asks me to implement logical negation, !, without using !. My idea is to use "~x+1", but the tester says this is incorrect. What am I doing wrong?
|
# ? Sep 29, 2008 00:13 |
|
|
# ? May 7, 2024 03:42 |
|
Comedy x ? false:true option.
|
# ? Sep 29, 2008 00:22 |
|
Mr.Mojo posted:I'm doing a problem that asks me to implement logical negation, !, without using !. My idea is to use "~x+1", but the tester says this is incorrect. What am I doing wrong? Do you understand the semantics of logical negation? Your proposal doesn't satisfy them for... well, for anything.
|
# ? Sep 29, 2008 01:10 |
|
rjmccall posted:Do you understand the semantics of logical negation? Your proposal doesn't satisfy them for... well, for anything. Oh duh, I was doing a normal negate, not a logical negate. Back to thinking about it!
|
# ? Sep 29, 2008 01:45 |
|
I'm fairly new and starting to work a lot with derived classes. I've got a main class with a virtual function that should never be called, and then several derived classes each with their own different versions of said function that should always be called instead. But, when I go through my queue calling my function on each class, it always calls the main class's function, not the derived class. Is there something simple that I'm missing?
|
# ? Sep 29, 2008 03:18 |
|
Post some code.
|
# ? Sep 29, 2008 03:20 |
|
Virtual function in the main class:code:
code:
code:
code:
|
# ? Sep 29, 2008 03:44 |
|
Since you're storing T in your queue, and not T*, what's happening is that you're actually storing a *copy* of the object you're adding. And because the queue is actually of type Point, it's using the copy constructor to create a new Point object, not a derived one, and copy just the Point data from the derived object you're adding. You need to store pointers inside your queue, and allocate the objects on the heap: code:
cronio fucked around with this message at 05:31 on Sep 29, 2008 |
# ? Sep 29, 2008 04:07 |
|
Dire Penguin posted:Queue<T> code:
* depending on how the copy constructor is defined, of course
|
# ? Sep 29, 2008 04:09 |
|
Awesome, that's exactly what I was doing. I went back through and changed the Queue to store T* and changed my declarations and the error goes away, as far as I can tell.
|
# ? Sep 29, 2008 04:36 |
|
Whoops, noticed I had a slight typo... I didn't mean to have Queue<T*> in there. What you really want to do is keep the queue holding T, but instantiate it with Shape* instead of Shape: code:
|
# ? Sep 29, 2008 05:34 |
|
Dire Penguin posted:Virtual function in the main class: If you make this function a pure virtual instead, it will 100% guarantee that the base version will never be called, because it doesn't even exist: code:
|
# ? Sep 29, 2008 06:03 |
|
JoeNotCharles posted:If you make this function a pure virtual instead, it will 100% guarantee that the base version will never be called, because it doesn't even exist: That's not true.
|
# ? Sep 29, 2008 09:30 |
|
floWenoL posted:That's not true. It is if you consider crashing with an error to be "not calling" it.
|
# ? Sep 29, 2008 14:52 |
|
JoeNotCharles posted:It is if you consider crashing with an error to be "not calling" it. He means that pure virtual functions can be defined and called. All that pure virtual implies is that the child should specify an implementation and makes the base type abstract, not that you can't also define the function in the base and explicitly call that implementationt. Case in point, pure virtual destructors, which in that case must be defined and are always called during destruction. That Turkey Story fucked around with this message at 20:56 on Sep 29, 2008 |
# ? Sep 29, 2008 20:53 |
|
Huh, I didn't know that. Yet another example of why C++ is so hard to master! Anyway, it will 100% guarantee that the base version will never be called automatically - is that better?
|
# ? Sep 29, 2008 21:11 |
|
JoeNotCharles posted:Anyway, it will 100% guarantee that the base version will never be called automatically - is that better?
|
# ? Sep 29, 2008 22:40 |
|
Dire Penguin posted:Virtual function in the main class: Yeah, C++ is supposed to have a feature that prevents this happening, but it doesn't loving work. Have fun!
|
# ? Sep 29, 2008 23:12 |
|
It isn't supposed to have any such feature. If it's been advertised to you as such, you've been misled. edit: I mean, I'm not blaming you for thinking that; that's just not what it was designed for. Mustach fucked around with this message at 23:39 on Sep 29, 2008 |
# ? Sep 29, 2008 23:32 |
|
Uh, I'm pretty sure it's for exactly this situation - you have an abstract base class (Shape), and you want to always call the member from a subclass and not the abstract base class. The issue is that C++ is so fiddly that I can't give a one-line description of pure virtual methods because, surprise, there are confusing corner cases. A pure virtual destructor isn't at all related to this situation because I wasn't advising him to use a destructor, but it still makes what I said incomplete and in C++ that's dangerous because there are so many features that interact that you have to be a huge rules lawyer to understand how they work. I mean, in any other language I'd say, "Pfft, you're being pedantic. Obviously I wasn't talking about destructors, and obviously if you actively try to make your pure virtual callable by implementing it after you declare it than you deserve what you get." But in C++ that kind of pedantry is actually necessary and admirable.
|
# ? Sep 30, 2008 00:16 |
|
code:
|
# ? Sep 30, 2008 02:35 |
|
So... you want it to be harder to find errors in debug mode?
|
# ? Sep 30, 2008 03:00 |
|
That Turkey Story posted:Case in point, pure virtual destructors, which in that case must be defined and are always called during destruction.
|
# ? Sep 30, 2008 14:54 |
|
more falafel please posted:
why would you do this
|
# ? Sep 30, 2008 17:22 |
|
I am having an issue trying to compile using g++. I am getting the following error message:code:
code:
|
# ? Sep 30, 2008 18:21 |
|
I'd need to see the definition of Game to be certain but it looks like you have: class Game { Player player; EventList events; Dungeon dungeon; }; So you can't create a Game without calling Player's constructor. To use a constructor other than Player(), use an initialisation list: Game::Game(string player_name) : player(player_name) { } Calling player = Player(...) inside the constructor is like doing: Player player; player = Player(...); It creates an instance using the default constructor, then creates another instance using another constructor and calls the assignment operator.
|
# ? Sep 30, 2008 18:40 |
|
Awesome, the initialization list fixed this, thanks! Just to get a better understanding of this, I declare Player player int the class declaration as a member variable. Upon calling the constructor, it calls the Player::Player() constructor rather than waiting and calling the Player::Player(string) that I define?
|
# ? Sep 30, 2008 18:50 |
|
yes
|
# ? Sep 30, 2008 18:56 |
|
ultra-inquisitor posted:I've read that pure virtual destructors should always be empty, but can't think of a reason for this? There is no reason, they should do whatever you need them to do.
|
# ? Sep 30, 2008 20:35 |
|
I recall there being an idiom in C++ for "template polymorphism", but I'm probably calling it by the wrong name, since Google isn't much help. I think TTS posted it in #cobol, if that helps any. Does anyone know what I mean?
|
# ? Sep 30, 2008 20:50 |
|
Avenging Dentist posted:I recall there being an idiom in C++ for "template polymorphism", but I'm probably calling it by the wrong name, since Google isn't much help. I think TTS posted it in #cobol, if that helps any. The curiously recurring template pattern?
|
# ? Sep 30, 2008 22:48 |
|
ehnus posted:The curiously recurring template pattern? Nah, but that's a fun one. The idea was that it worked somewhat like a virtual functor, but there was some template magic in place of the virtual function call. I don't even know if I'd have any use for it, but I was thinking about it the other day and couldn't find it again. EDIT: it looked something like this code:
Avenging Dentist fucked around with this message at 23:10 on Sep 30, 2008 |
# ? Sep 30, 2008 23:05 |
|
I don't know if there is a special name for it, I think the common term is just vaguely "static polymorphism" which could apply to a number of different things. You do generally use the curiously recurring template pattern to do it, however.code:
|
# ? Oct 1, 2008 03:04 |
|
Also seen it called "compile-time polymorphism".
|
# ? Oct 1, 2008 15:08 |
|
I'm just getting started with gcc's built-in vector extensions, and I want to perform the following operation on a vector:code:
code:
code:
I'm kind of stuck with gcc 4.1.2 as I'm on Debian stable, but if this is "fixed" in later versions I might have reason enough to lobby for a version update.
|
# ? Oct 1, 2008 15:28 |
|
That's correct because the concept of vector immediates generally doesn't exist. You'll need to splat that value across a vector variable before you can use it
|
# ? Oct 1, 2008 16:53 |
|
That's poopy. I wanted to use very large vectors for readability (and a few other minor reasons), and there are a minimum of 5 different constant values I need to use (depending on the vector). That's going to eat a ton of memory.
|
# ? Oct 1, 2008 17:11 |
|
Intro c++ here... I'm trying to write a code that simply copies one word in a char array over to another, using pointers. I think I'm pretty close, but when I cout << str2 in my main function, all that comes out is garbage for some reason, even though the correct output comes when I cout << str2 in my copyString function. Any hints would be nice. code:
Soldat fucked around with this message at 00:28 on Oct 2, 2008 |
# ? Oct 2, 2008 00:14 |
|
Furthermore, in 99% of situations (especially in C++), it's bad practice to allocate memory in a function and then expect the calling function to free that memory.
|
# ? Oct 2, 2008 00:21 |
|
|
# ? May 7, 2024 03:42 |
|
Sorry I should have clarified, I think we're only supposed to use char arrays. I can post the spec if that helps: code:
str2 = new char [size+1]; EDIT: nevermind, thanks for the advice dentist. I changed where I was allocating space and tweaked a couple of other things, it works now. Soldat fucked around with this message at 00:39 on Oct 2, 2008 |
# ? Oct 2, 2008 00:27 |