Overloading a variable with multiple data types

This is a discussion on Overloading a variable with multiple data types within the C++ Programming forums, part of the General Programming Boards category; I also agree with NeonBlack and Creativ3. Exposing someone to more arcane knowledge or potentially dangerous tools before they are ...

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    I also agree with NeonBlack and Creativ3. Exposing someone to more arcane knowledge or potentially dangerous tools before they are ready for it is the way to get them into trouble.

    The military, for example, don't throw a loaded machine gun to new recruits on the first day. They actually take them through cleaning an unloaded gun, checking it is unloaded, and a few other things before ever handing the student a bullet.

    The most effective teachers I know start simple, teach good habits, but also let the student believe things are a little less flexible than they really are. Once the student is better grounded with the basics, those teachers then point out the minor omissions (eg the more advanced techniques) and explain both the reasons the things were omitted before and why the student is (now) being exposed to them.

    The least effective teachers I know insist on dumping all the advanced material on the students in no particular order - and they are often more concerned with demonstrating their own knowledge rather than helping the student learn.

    Most people, naturally, fall between those extremes.
    Right 98% of the time, and don't care about the other 3%.

  2. #17
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    It is apparent we have C++ purists here who feel that all C constructs are not useful. However there are times when a C approach inside of a C++ class is the best way to approach a problem.

    I do not use C++ to be a C++ guru - I use C++ to solve problems and if C++ does not adequately solve the problem or makes a simple problem more complicated then I do not use it's constructs. If that makes me a bad programmer then so be it - I'm a bad programmer.

    Unions are very useful in certain situations. Usually you will see unions used in cooperation with some type of scripting language where you must pass a variable from C/C++ to the scripting system or have data types coming in from the script system that must be converted to C/C++ data types. Templates cannot be used here b/c you don't want to be able to specify any data type but a specific set of data types - namely the native C/C++ data types.


    If you look at each piece of C and C++ as a tool in your arsenal of tools then I think you will avoid the 'don't use this C construct or you are a bad programmer' mindset. They are all tools that can be utilized by the programmer. And if unions are supported by the compiler then I see no problem in using them. It is my personal opinion that a good programmer will use all of the tools available to him rather than try to be a purist at the expense of increased development time. Quite simply if the answer is staring you in the face then use it. I've seen a lot of programmers who will bang their head against a wall to avoid a C-style contruct or function only to come up with a solution that is ten times more complex and risky. It is true we all attempt to find the best solution but sometimes the simplest solution is the best. Again you must leverage your own judgement and design skills as well as experience to make these kinds of calls. But then again that is why they call us programmers and software engineers. It's not always as black and white as always do this or always don't do this. It really depends on the situation.

    In this situation the OP is looking for a variant and we do not have one in C or C++. In this case I would say there are plenty of other solutions to the problem at hand than a union. The union serves a specific purpose and yet this particular situation does not require a union. This situation is causes by a misunderstanding of the language. Each variable must have a specific type and nothing can get around that. There is no reason to use a union here.

    cout << "Type in an integer or string: ";
    I can think of about a million things that are wrong with this type of question. But if it is required I can also think of at least ten ways to differentiate between string and int using C/C++.
    But ask yourself why are you asking the user to type in an integer or a string? What data is in an integer that would also be in a string given they are two completely different unrelated data types? I would question the design of the program here. What are you trying to do and why? Re-think it and you will end up with a better solution. If the user types in a number and you store it as a string then you can extract the number from the string. If the user types in a string then you can store it in a string as well. Hmm.
    Last edited by VirtualAce; 02-13-2010 at 12:28 PM.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    For the record, I do agree with you, Bubba.
    I don't have anything against unions, but for a newbie programmer, it may not be the best thing to be dumped at them in the beginning. They come from C, and are therefore dangerous unless used properly. More so than C++ constructs.
    Your analysis of the solution is a good one and is something that I was thinking, as well.
    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. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    Quote Originally Posted by Bubba View Post
    It is apparent we have C++ purists here who feel that all C constructs are not useful.
    Nobody suggested C constructs are not useful.

    The discussion concerned differences of view about whether beginners should be arbitrarily exposed to particular constructs before they have a grounding in the basics. Several constructs in both C and C++ are dangerous if misused, and encouraging their uninformed use does a disservice to beginners.

    Other than that, I mostly agree with your analysis.

    In practice, unions and variant types (assuming you have a library that supports them) are easy to misuse. They have both advantages and problems if used in an uninformed manner.
    Right 98% of the time, and don't care about the other 3%.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 04:29 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. data types help
    By Bud in forum C Programming
    Replies: 2
    Last Post: 06-27-2002, 12:35 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

Tags for this Thread


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