Like Tree1Likes

Advantages of using C first?

This is a discussion on Advantages of using C first? within the General Discussions forums, part of the Community Boards category; In 99% of the cases, you don't need to know the type. You don't need to discover the type. If ...

  1. #31
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    In 99% of the cases, you don't need to know the type. You don't need to discover the type.
    If you think you have an example of where it might be useful, feel free to post it. Usually, there is an elegant solution without RTTI.
    For example, take a look at the standard library: it uses templates almost exclusively. I have never seen any use of RTTI, yet it works perfectly.
    There are drawbacks to RTTI too, because it allows you identify a specific type, but there can be an infinite amount of types, which means adding support to each and every one of them.
    Not a really good idea. Instead you can generalize it by stating that the type should support a common interface and work from that. It means that all types that has this interface works with the code, regardless of type.
    Last edited by Elysia; 03-26-2010 at 02:34 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #32
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Quote Originally Posted by Elysia View Post
    In 99% of the cases, you don't need to know the type. You don't need to discover the type.
    If you think you have an example of where it might be useful, feel free to post it. Usually, there is an elegant solution without RTTI.
    Ok. There must be some confusion here. I usually trust your knowledge of C++, so my guess is you are distracted, I am distracted, or we are talking different things entirely. My vote is in the first, sorry.

    RTTI is a feature of the language. It is activated the moment you create a pointer or reference to a base class with at least one virtual method. There's nothing you can do about that. If you are trying to say that in 99% of the cases you are never going to create a pointer or reference to a base type with at least a virtual method, you will not succeed in convincing me.

    EDIT: are you by any chance talking of dynamic_cast and typeid?
    Last edited by Mario F.; 03-26-2010 at 02:42 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Ah. When I think of RTTI, I think of the typeid operator.
    Yes, typeid is bad. Virtual methods are definitely not (nor dynamic_cast).
    (But use dynamic_cast with caution.)
    Last edited by Elysia; 03-26-2010 at 02:43 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by Mario F.
    RTTI is a feature of the language. It is activated the moment you create a pointer or reference to a base class with at least one virtual method.
    RTTI is a language feature, but when people talk about RTTI, they usually mean the use of dynamic_cast and/or typeid, not virtual functions.

    EDIT:
    In fact, Stroustrup's C++ glossary entry on run time type information suggests "dynamic_cast, typeid(), and type_info" for further reading, with no mention of virtual functions.

    EDIT #2:
    Furthermore, g++'s -fno-rtti option disables "generation of information about every class with virtual functions for use by the C++ runtime type identification features (`dynamic_cast' and `typeid')."
    Last edited by laserlight; 03-26-2010 at 02:48 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #35
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Quote Originally Posted by laserlight View Post
    RTTI is a language feature, but when people talk about RTTI, they usually mean the use of dynamic_cast and/or typeid, not virtual functions.
    Quote Originally Posted by Elysia View Post
    Ah. When I think of RTTI, I think of the typeid operator.
    Yes, typeid is bad. Virtual methods are definitely not (nor dynamic_cast).
    (But use dynamic_cast with caution.)
    Ah, that was my guess when I stated looking for what would make you say that. I personally never think of RTTI as simply dynamic_cast and typeid and I'm not sure "people" do too. In fact I think of dynamic_cast and typeid as extensions to RTTI that allow programmers an alternate method to gather the internal type of an object.

    In any case, I would never say RTTI should be avoided.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #36
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Well, we have our different views.
    typeid should usually be avoided since it hard codes a path for one type.
    And when you have to retort to dynamic_cast, you really should be asking yourself if your design is sound, because usually casts of this type can be avoided in C++. Sometimes there are legitimate reasons to use it, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #37
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    No, you misunderstood me. I do not say those things shouldn't be avoided. I say RTTI is more than those things.

    I actually made a mistake on post 32. Was writing it too fast and probably already germinating in my head you could be talking of typeid and dynamic_cast. RTTI is activated the moment you create a pointer or reference to a base class, period. Not just base classes with virtual methods. So, RTTI extends well beyond these two operators and their capabilities.

    It's possible people do think of RTTI only as those two. But that is a surprise to me. But for the sake of correctness, do not say RTTI should be avoided. Ok?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #38
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    To avoid typeid(), how would you go about idenifying the object type like this example? Surely there is an alternative? It's one advantage to creating a factory function and keeping track of what mammal you created, what monster was randomly generated, etc.

    Code:
    Mammal *ptr;
    
    for (int i = 0; i < 10; i++) { 
      ptr = factory();
    
    if (typeid(*ptr) == typeid(Puppy)) { // it's a puppy! }
    }

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by Mario F.
    RTTI is activated the moment you create a pointer or reference to a base class, period. Not just base classes with virtual methods. So, RTTI extends well beyond these two operators and their capabilities.
    Could you provide a reference that backs up this assertion? I have provided you with references that imply that Stroustrup and the g++ manual do not have that in mind. If those were not enough, read this interview of Scott Meyers on When to Use RTTI.

    Quote Originally Posted by dxfoo
    To avoid typeid(), how would you go about idenifying the object type like this example? Surely there is an alternative?
    It depends on the situation. The article that I linked to above partially answers your question (i.e., use a virtual function and polymorphism). It may be the case that something more elaborate is needed, e.g., an application of the visitor pattern. Or... you may have to bite the bullet and use RTTI with a dynamic_cast.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #40
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    typeid() seems fine to me when I use it for this purpose. I'd like to see a code example of an alternative approach though.

  11. #41
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    For reference, here's the full code example. I don't see a disadvantage using it so far.

    Code:
    #include <iostream> 
    #include <fstream>
    #include <string>
    #include <typeinfo>  
    
    class Mammal 
    {
    public: 
    };
    
    class Cat : public Mammal
    {
    public:
    };
    
    class Platypus : public Mammal 
    {
    public: 
    };
    
    class Dog : public Mammal
    {
    public:
    };
    
    Mammal *factory() 
    {
    	switch (rand()%3) 
    	{
    	case 0: return new Dog;
    	case 1: return new Cat;
    	case 2: return new Platypus;
    	}
    
    	return 0;
    }
    
    int main()
    {
    	Mammal *ptr; 
    	int i;
    	int c = 0, d = 0, p = 0;
    
    	for (i = 0; i < 10; i++) 
    	{
    		ptr = factory();
    
    		std::cout << "Object is " << typeid(*ptr).name() << std::endl;
    		std::cout << std::endl;
    
    		if (typeid(*ptr) == typeid(Dog)) 
    			d++;
    		if (typeid(*ptr) == typeid(Cat)) 
    			c++;
    		if (typeid(*ptr) == typeid(Platypus)) 
    			p++;
    	}
    
    	std::cout << "Cats: " << c << std::endl;
    	std::cout << "Dogs: " << d << std::endl;
    	std::cout << "Platypus: " << p << std::endl;
    
    	return 0;
    }

  12. #42
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by dxfoo
    For reference, here's the full code example. I don't see a disadvantage using it so far.
    In this specific case, you do not really need it. For example, if you intend to globally track the number of objects of each Mammal subtype, you could use static member variables (but beware of initialisation issues). If you only intend to track the number of those created in the loop, you could have passed by reference a struct of counts that is incremented accordingly.

    By the way, Mammal should have a virtual destructor, and you forgot to delete what you new.

    EDIT:
    Using a struct of counts might be seen as a cop out, I guess, but it really is a straightforward solution, at the cost of coupling the creation of objects with counting them. I am rather hesitant to suggest applying the visitor pattern in this case though, since for so trivial an example it would look like a whole lot of unnecessary work. Just using virtual functions probably will not be good enough, since your aim is to find out about each subtype (i.e., something inherently along the lines of type switching), rather than just let them perform some polymorphic operation. We could use virtual functions with a struct of counts though.

    On a more conceptual level, the bad part of using typeid and dynamic_cast is that it usually means that you do not benefit from the open-closed principle (PDF) as you would need to accomodate a new subtype in many places. (Effectively, you would be doing manual type switching.)
    Last edited by laserlight; 03-26-2010 at 04:11 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #43
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Quote Originally Posted by laserlight View Post
    Could you provide a reference that backs up this assertion? I have provided you with references that imply that Stroustrup and the g++ manual do not have that in mind. If those were not enough, read this interview of Scott Meyers on When to Use RTTI.
    I sure am having an hard time doing it, laserlight. And I've been trying since post #34. Particularly the Artima article is the most damaging.

    My understanding of the meaning of RTTI was from very early been defined as a generic term meaning the ability to know the actual type of an object (at runtime and not just for the user but for the runtime environment in general). *Explicit* RTTI (dynamic_cast and typeid) being simply a part of the overall picture, that included implicit derived to base conversions.

    I guess I will need to reevaluate this.
    Last edited by Mario F.; 03-26-2010 at 03:59 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #44
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Ah, so while calling factory(), maybe a reference variable aka factory(&objFactory) can let you know what was just generated...

  15. #45
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    It is my opinion if you design correctly you won't need to use typeid(). There is nothing in the language that forces you to use it. Asking an object about it's type should not be needed and should be discouraged since types can change. The only thing I can see coming after such a request is a nasty if or switch based on the return type and I shudder to think about the repercussions of such a design. There may be times where you have to use dynamic_cast but usually this can be avoided. I have never used typeid() other than for test harnesses and debugging purposes. I find it of little use in actual production code.

    IMO there is no advantage to learning C before C++. C++ is so much more than C with classes and if you design with that in mind you will have a very hideous class structure. C is all about making one piece do everything and C++ is about spreading out functionality across a broad spectrum of objects devoted to doing one or two important things while having as little knowledge of and dependencies on other objects. All problems can be solved easily given there is a sufficient level of abstraction present. If the problem is too complex then further abstraction can simplify it. C++, for me, is all about abstraction and knowing when to abstract and when not to. Too much abstraction results in a messy hierarchy and too little results in too many dependencies and clunky and unwieldy interfaces.
    Last edited by VirtualAce; 03-26-2010 at 04:47 PM.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-24-2009, 01:28 AM
  2. advantages of using IDE
    By surrounded in forum C Programming
    Replies: 8
    Last Post: 03-03-2009, 05:36 AM
  3. Advantages of using macro for BIT positions
    By donmorr in forum C Programming
    Replies: 8
    Last Post: 07-23-2008, 06:15 AM
  4. advantages of tile-based engine...
    By o0obruceleeo0o in forum Game Programming
    Replies: 25
    Last Post: 07-22-2003, 08:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21