Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7431707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:20:16+00:00 2026-05-29T09:20:16+00:00

I have learned that type fields is an error-prone technique somewhere I have Google

  • 0

I have learned that
type fields is an error-prone technique somewhere

I have Google it much but no satisfactory results .Although I know the meaning of type and fields individually . I am thinking that type field means field of a particular type like

class marks;
class test {
marks number;
bool pass;
}

Here according to me type field is number and pass .
If this is right how is composition different from it?
Also what is type field technique?? How it is error prone??

Please give it true meaning. Also give an example to show its evil nature and what are its substitutes ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:20:17+00:00Added an answer on May 29, 2026 at 9:20 am

    You’re almost certainly referring to type fields as discussed in the book The C++ Programming Language by Bjarne Stroustrup. A type field in this context would simply be a variable of some kind in a base class that indicates the actual type of its subclasses. Here’s an example:

    class Pet
    {
    public:
        enum PetType { Dog, Cat, Bird, Fish };
    
        void ToString()
        {
            switch(type)
            {
            case Pet::Dog: std::cout << "Dog" << std::endl; break;
            case Pet::Cat: std::cout << "Cat" << std::endl; break;
            case Pet::Bird: std::cout << "Bird" << std::endl; break;
            case Pet::Fish: std::cout << "Fish" << std::endl; break;
            }
        }
    
    private:
        PetType type; // A type field.
    };
    
    class Dog : public Pet
    {
    public:
        Dog() { type = Dog; }
    };
    
    // And so on...
    
    void Test(const Pet& p) { p.ToString(); }
    int main()
    {
        Dog d;
        Test(d);
        return 0;
    }
    

    This is an extraordinarily brittle way to implement a ToString() method. Every time you need to add a derived class of Pet, you would need to update the PetType enumeration and the ToString() method. For example, if I need a Turtle subclass, I would need to make these changes:

    // ...
    enum PetType { Dog, Cat, Bird, Fish, Tutle /* Added */};
    void ToString(const Pet& p)
    {
        switch(p.type)
        {
        case Pet::Dog: std::cout << "Dog" << std::endl; break;
        case Pet::Cat: std::cout << "Cat" << std::endl; break;
        case Pet::Bird: std::cout << "Bird" << std::endl; break;
        case Pet::Fish: std::cout << "Fish" << std::endl; break;
        case Pet::Turtle: std::cout << "Turtle" << std::endl; break; // Added
        }
    }
    // ...
    class Turtle : public Pet
    {
    public:
        Turtle() { type = Turtle; } // Added
    };
    

    Imagine if the Pet class had more functions than just ToString(); maintenence becomes a nightmare. It’s lot of code one needs to change, but the important thing is that in order to have a Turtle class, I need to modify the Pet class. That means more testing, code review, etc. is needed. It’s a clear violation of the open/closed principle. That’s why type fields are extremely error-prone.

    A significantly superior way would be to use virtual functions:

    class Pet
    {
    public:
        virtual void ToString() = 0;
    };
    
    class Dog : public Pet
    {
    public:
        virtual void ToString() { std::cout << "Dog" << std::endl; }
    };
    
    class Turtle : public Pet
    {
    public:
        virtual void ToString() { std::cout << "Turtle" << std::endl; }
    };
    
    // And so on...
    
    void Test(const Pet& p) { p.ToString(); }
    int main()
    {
        Turtle t
        // Will call Turtle::ToString(), even though
        // Test() was only given a const Pet&
        Test(t); 
        return 0;
    }
    

    Note that the above code requires no extra enums or switch statements. Calling Pet::ToString() will call the correct implementation of ToString() for Dogs, Cats, etc. automatically, with much less code. I don’t even need to change the Pet class; I can just drop in a Turtle class if needed, provided that Pet has been defined.


    For a possibly legitimate use of type fields, see this Stack Overflow question and the answers to that question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have learned so much from http://www.summerofnhibernate.com/ nhibernate screen casts that i wonder why
I have two questions: (1) I learned somewhere that -O3 is not recommended with
I've learned in College that you always have to free your unused Objects but
After much research, I have learned that binding the scroll event to the $(window)
I have learned that we can't instantiate an abstract class. But today i tested
So I have learned that that the Microsoft.Jet.OLEDB.4.0 data provider for querying data sources
I'm required to write a JCE provider. I have learned that I need to
One CSS rule I have learned is that you should use the relative em
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead
I have an application that stores images in a database. Now I have learned

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.