|
What does an abstract class with no public interface give you over just not inheriting at all? void doStuff(VehicleDealership vd) { ... } can't actually do anything with vd.floWenoL posted:What if someone has a PorscheDealership but add()s a non-Porsche car? It'd fail to compile because there is no add(Car *)?
|
# ? Feb 13, 2009 04:02 |
|
|
# ? Apr 25, 2024 20:59 |
|
awesmoe@batman:~$ cat test.ccpre:class Foo { int i; }; int main(int argc, char** argv){ Foo* foo = dynamic_cast<Foo*>(foo); // 1 int i = i; // 2 } awesmoe@batman:~$ Is anyone else surprised that neither of the lines in main give at least warnings? Is this a bug in g++ or is it standard behaviour, and if it's standard can anyone give me insight into why it was decided upon?
|
# ? Feb 13, 2009 04:13 |
|
awesmoe posted:Is anyone else surprised that neither of the lines in main give at least warnings? Why would you think that's a bug in the compiler and not your code? Incidentally, you want -Winit-self Avenging Dentist fucked around with this message at 04:19 on Feb 13, 2009 |
# ? Feb 13, 2009 04:16 |
|
Plorkyeran posted:It'd fail to compile because there is no add(Car *)? Well, I meant addCar() but looks like that's protected. I guess by "pure virtual" ThugBonnet meant that base classes wouldn't have any methods at all, which is pretty useless, then.
|
# ? Feb 13, 2009 04:19 |
|
Avenging Dentist posted:Why would you think that's a bug in the compiler and not your code? ed: Still nothing with -Winit-self awesmoe fucked around with this message at 04:24 on Feb 13, 2009 |
# ? Feb 13, 2009 04:22 |
|
awesmoe posted:I mean bug in the compiler to accept that code. As I see it, that code shouldn't compile. Is there a specific legitimate case where someone would use that construct (necessitating compilers accepting it) , or is it just one of those bugs that the compiler doesn't warn you about? Avenging Dentist posted:Incidentally, you want -Winit-self
|
# ? Feb 13, 2009 04:23 |
|
awesmoe posted:ed: Still nothing with -Winit-self Avenging Dentist fucked around with this message at 04:35 on Feb 13, 2009 |
# ? Feb 13, 2009 04:32 |
|
Plorkyeran posted:What does an abstract class with no public interface give you over just not inheriting at all? void doStuff(VehicleDealership vd) { ... } can't actually do anything with vd. In practice there would be public interfaces for the base classes, sorry that wasn't clear, I was just illustrating the hierarchy. And it's okay if the lists in the base classes are out of sync with one another. For example, there could be a GermanCarDealership which inherits from both PorscheDealership and VolkswagenDealership, in which case the list of Car* in CarDealership will have more elements than either the list of Porsche* in PorscheDealership or the list of Volkswagen* in VolkswagenDealership. Because of this we can safely call public methods at any level of specificity (Vehicle, Car, Porsche, Volkswagen) without casting, we can enumerate our Porsche*s without a dynamic_cast, etc. As far as how this is important to my GamePresenter and GameView, they inherit from Observer and Subject respectively. I would like those two base classes to handle everything that needs to be done for the Observer-Subject relationship: Subjects may be observed by any number of Observers, and handle the basic methods they call on one another for basic messaging and registration. But in certain cases I would like to prevent some specific couplings of Observer-Subject in cases where it wouldn't make any sense, and do so without relying on RTTI. It wouldn't make much sense for a GamePresenter to observe a WindowView, for example. Thug Bonnet fucked around with this message at 05:44 on Feb 13, 2009 |
# ? Feb 13, 2009 05:37 |
|
Like I said a long time ago, use templates.
|
# ? Feb 13, 2009 06:04 |
|
Thug Bonnet posted:Lots of problems Read through this (21.6 through to 21.11): http://www.parashift.com/c++-faq-lite/proper-inheritance.html and make sure you understand the substitution principle of inheritance before proceeding.
|
# ? Feb 13, 2009 10:50 |
|
TSDK posted:Your root problem is that you're trying to apply real-world intuition about the relationships between different objects to class design and inheritance. Just because a Car is-a type of Vehicle in the real world, it doesn't hold that mirroring this directly into a class hierarchy will result in a good (or even workable) design for what you're trying to write. This holds just as true for GamePresenter etc... I thought I had a pretty-good handle on the is-a problem, especially with regard to the circle/ellipse example. Even given the circle/ellipse example it would seem that as far as a hierarchy goes that you can use Vehicle->Car without too much problem, as long as you don't assume anything in Vehicle that may dilute Car. You wouldn't want an openDoor() method in Vehicle, for example, since lots of vehicles don't have doors. What about the Vehicle->Car (or GameView-GamePresenter) relationship do you feel is inherently improper?
|
# ? Feb 13, 2009 11:58 |
|
awesmoe posted:I mean bug in the compiler to accept that code. As I see it, that code shouldn't compile. Is there a specific legitimate case where someone would use that construct (necessitating compilers accepting it) , or is it just one of those bugs that the compiler doesn't warn you about? code:
Mustach fucked around with this message at 16:30 on Feb 13, 2009 |
# ? Feb 13, 2009 16:27 |
|
awesmoe posted:I mean bug in the compiler to accept that code. As I see it, that code shouldn't compile. Is there a specific legitimate case where someone would use that construct (necessitating compilers accepting it) , or is it just one of those bugs that the compiler doesn't warn you about? I could try to think up an example where "void *x = &x;" is useful. Since compilers have to allow "&x" on the right hand side they might as well allow "x". Specifically using the value of x in the definition of x? You're asking for an example of code that makes use of uninitialized values, and undefined values are useless (except perhaps as entropy). But imagine a code generator that sometimes writes "int a = a;" and never uses "a". Perhaps this makes the code generator cleaner. Thanks to the standard, a conforming compiler is able to handle this code. It is much nicer to have a compiler that sticks to a standard and gives optional warnings on human-readable code.
|
# ? Feb 13, 2009 17:08 |
|
Thug Bonnet posted:What about the Vehicle->Car (or GameView-GamePresenter) relationship do you feel is inherently improper? However, the substitution principle says that wherever you have a Base *, then it must be acceptable to pass a Derived *. The container classes are heading towards breaking that principle, and everything else from then on is just a sticking plaster. Given the Car/Dealer scenario you've outlined, a better design would be to use data rather than classes to drive the behaviour. So you have a single Vehicle class with a type ID (possibly an enum), and the Dealer class then has a member saying which type ID (or IDs) is acceptable to be stored in the container. This is effectively boils down to manually doing what the dynamic_cast testing would be doing anyway, but it's a lot less code to add a new type ID than it is to add in a new derived class, and you're making it much more explicit what's actually going on.
|
# ? Feb 13, 2009 18:06 |
|
TSDK posted:However, the substitution principle says that wherever you have a Base *, then it must be acceptable to pass a Derived *. The container classes are heading towards breaking that principle, and everything else from then on is just a sticking plaster. That's what I was working toward, the multiple lists is just to prevent casting up, or using an enum, but I'll look at enums again and see if it makes more sense to me having re-read the faq-lite OO section.
|
# ? Feb 13, 2009 21:25 |
|
You shouldn't need to use enums here; it should be perfectly possible to do this in a type-safe manner. However, you will need to convince yourself that there are two valid but different kinds of polymorphism, and that this case requires both. Let's go back to your dealership scenario. You've got vehicle dealerships, car dealerships, and Porsche dealerships, and you think you want them to be related in a chain of subtypes. That's not inherently unreasonable, but it really comes down to your definitions, so let's look at those a little. What is a vehicle dealership? It's a place where you buy and sell vehicles, right? Well, we could define it that way: "a vehicle dealership is a place where you can buy and sell any kind of vehicle." Now, under this definition, is a Porsche dealership a vehicle dealership? No, it isn't — a Porsche dealership lets you buy and sell one particular kind of vehicle, not arbitrary vehicles. So if our definition doesn't work for what we want to do, let's find a better one. "A vehicle dealership is a place where you can buy and sell a particular kind of vehicle." That's an example of "parametric polymorphism"; now we can't just talk about dealerships, we have to say what kind of things the dealership trades in. So a vehicle dealership is a dealership that trades in (some type of vehicle), and a car dealership trades in (some kind of car), and a Porsche dealership trades specifically in Porsches. In C++, you use templates to express parametric polymorphism; unfortunately, the type system is (currently) not sophisticated enough (I think) to statically check quite everything you want to check. So you can write this: code:
|
# ? Feb 13, 2009 22:10 |
|
Thanks for the replies, guys - I'm still surprised that int i(i); works, but I can understand the other cases. I don't understand order of execution in construction very well at any rate (as demonstrated here!), so thanks for the help. Incidentally -Winit-self was only added in gcc 4.0, and I was using 3.2.3 when I found the actual error in the code. So I guess I wasn't the only one who thought a warning should be added for that case And AD - the reason I specifically asked here rather than just googling 'assignment during initialization' was because I'm pretty sure there's some idea that I'm missing here, and I thought that a dialogue would let me get a clearer picture of what it is than just a wall of text.
|
# ? Feb 13, 2009 23:05 |
|
rjmccall posted:In C++, you use templates to express parametric polymorphism; unfortunately, the type system is (currently) not sophisticated enough (I think) to statically check quite everything you want to check. You can with the power of SFINAE (or in C++0x with concepts)! With boost this can be done with Boost.TypeTraits and enable_if: code:
|
# ? Feb 14, 2009 04:14 |
|
Yeah, that's why I qualified that first statement. I should've known that one, though.
|
# ? Feb 14, 2009 04:25 |
|
That Turkey Story posted:You can with the power of SFINAE (or in C++0x with concepts)! With boost this can be done with Boost.TypeTraits and enable_if: haha, "is_convertible" for cars
|
# ? Feb 14, 2009 04:35 |
|
Ugg boots posted:haha, "is_convertible" for cars
|
# ? Feb 14, 2009 04:49 |
|
Quick question, is it possible to make a non-member operator[] function?code:
What I want to be able to do is index a two dimensional array using a pair instead of having to drag out .first and .second, like this: code:
|
# ? Feb 14, 2009 08:52 |
|
No.
|
# ? Feb 14, 2009 08:56 |
|
Actually I just thought about it for a second and did this, which works I guess:code:
|
# ? Feb 14, 2009 09:27 |
|
There are a thousands of posts in this thread and search is not available at the moment, so forgive me if this something covered a lot. I have a pgina dll that's used at work. I'm trying to add functionality to it so that a remote system can instruct different computers to do things (computer lab setting, so the lab attendant can log many stations in without having to do them all individually, stuff like that). I would think this would be possible by setting up a thread in the dll which listens for instructions over the network. I have a stand alone program which correctly accepts instructions from the network and does the appropriate actions, but when i throw that code into the dll project (vc++), my server thread gets as far as the accept() function from winsock2.h. code:
There is existing code in the dll which sends commands/requests out over the network using winsock2, and I have made sure that my server is running on a different port than those commands (unrelated to the functionality i'm trying to add). I don't plan on posting more code since it works outside of the dll, I'm just not sure what to look for inside of the dll to see what could be messing it up. Any suggestions what I could look for, what things may cause that accept call to fail without any errors being thrown? Or do I need to post more code?
|
# ? Feb 15, 2009 01:43 |
|
Alright so, I have a serious issue which is bugging me and I can't find the answer.I have a function in a class that when it is called sets a flag to 'true' and sets a pointer in the object calling the function equal to the object passed into it. The only issue is, is that after the function that sets the pointer is called the objects destructor is called and my pointer is deleted. Any ideas as to what is causing this?
|
# ? Feb 16, 2009 11:19 |
|
You are passing something by value instead of by reference?
|
# ? Feb 16, 2009 11:34 |
|
Vanadium posted:You are passing something by value instead of by reference? nope, it is passed by reference code:
Cirrus_Alreia fucked around with this message at 12:21 on Feb 16, 2009 |
# ? Feb 16, 2009 11:37 |
|
Is there anything in C++ that is like a php string-indexed array or javascript object or dictionary thing? I am taking my first C++ class and I've been asked to do one of 5 different things based on a string input. I know that I can't dynamically choose a function based on a string, but since there's no in_array function in C++ I can't think of how to validate the input without writing a lot of if's. Basically I'm trying to do this in java script: code:
Thanks.
|
# ? Feb 16, 2009 19:03 |
|
sonic bed head posted:Is there anything in C++ that is like a php string-indexed array or javascript object or dictionary thing? That would be a std::map<std::string, T>. code:
shrughes fucked around with this message at 19:25 on Feb 16, 2009 |
# ? Feb 16, 2009 19:23 |
|
Is there some sort of built in profiler for VS 2005? I can't seem to get gprof working at the moment and figured now is as good a time as any to learn how to use a new tool.
|
# ? Feb 16, 2009 19:31 |
|
shrughes posted:That would be a std::map<std::string, T>.
|
# ? Feb 16, 2009 19:48 |
|
Contero posted:Is there some sort of built in profiler for VS 2005? I can't seem to get gprof working at the moment and figured now is as good a time as any to learn how to use a new tool. There is, but only in the Team Edition.
|
# ? Feb 16, 2009 20:19 |
|
Dijkstracula posted:Indeed, but if this is an assignment, going overboard with STL containers and other stuff that he's guaranteed to not have been taught will probably not result in him getting a good grade. A real real easy way to know this for sure is to just email a TA and ask if you can use poo poo
|
# ? Feb 16, 2009 21:24 |
|
Otto Skorzeny posted:A real real easy way to know this for sure is to just email a TA and ask if you can use poo poo Easier to ask forgiveness than permission!
|
# ? Feb 16, 2009 21:24 |
|
sonic bed head posted:I am taking my first C++ class and I've been asked to do one of 5 different things based on a string input. sonic bed head posted:I know that I can't dynamically choose a function based on a string sonic bed head posted:but since there's no in_array function in C++ I can't think of how to validate the input without writing a lot of if's. code:
sonic bed head posted:What is the best practice for doing something like this? Should I really be writing, if(input != "first && input !="second" &&..)? I'd imagine the easiest thing to do would be to have some kind of lookup table mapping user input to actions. There are lots of different ways you could do this, and many different libraries you could leverage to accomplish it. But for a simple class assignment a few ifs is probably good enough. You don't absolutely need to do like you suggested though: "if(input != "first && input !="second" &&..)" is a bit overkill. You could simply have the final else catch all invalid input.
|
# ? Feb 16, 2009 21:32 |
|
GrumpyDoctor posted:There is, but only in the Team Edition. Well at least I got gprof working.
|
# ? Feb 16, 2009 21:35 |
|
In MSVC, is there a way to tell if you're running in the debugger? On a related note, why would an app take ~5 times as long to load in the debugger as outside it?
|
# ? Feb 16, 2009 21:40 |
|
more falafel please posted:On a related note, why would an app take ~5 times as long to load in the debugger as outside it? It might have something to do with the debugger attempting to load symbols for every module loaded by your application, which of course would not happen outside the debug environment. If you're set up to pull Windows symbols from the Microsoft symbol servers automatically instead of on-demand, that process can take a really long time.
|
# ? Feb 16, 2009 21:52 |
|
|
# ? Apr 25, 2024 20:59 |
|
csammis posted:If you're set up to pull Windows symbols from the Microsoft symbol servers automatically instead of on-demand, that process can take a really long time. This means you've debugged WPF applications before! </offtopic>
|
# ? Feb 17, 2009 03:38 |