How to handle abstract classes?

This is a discussion on How to handle abstract classes? within the C++ Programming forums, part of the General Programming Boards category; I have defined an abstract class CTermino... in one part of the code I wrote: Code: void agregarReemplazo (CTermino *terOriginal, ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    15

    How to handle abstract classes?

    I have defined an abstract class CTermino... in one part of the code I wrote:

    Code:
    void agregarReemplazo (CTermino *terOriginal, CTermino *terReemplazante)
    //Stuff
      pair < CTermino*, CTermino* > nuevoReemplazo (terOriginal, terReemplazante);
    Compiling that I got:
    error: cannot declare field 'std:air<CTermino, CTermino>::first' to be of abstract type 'CTermino'

    Is it posible to declare a pair<,> of an abstrac type? What can I do to solve that error??

    Thanks in advance!!!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The error message does not correspond to the code you posted. In the error message, the pair's members are objects, in the code snippet they're pointers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    A pair holds instances of types. An abstract type cannot be instantiated. Thus, an abstract type cannot be in a pair.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    Thatīs true... my mistake... The code I posted correspond to another try... :S
    Thereīs no any chance I could possible do what Iīm trying to do?

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    A pair holds instances of types. An abstract type cannot be instantiated. Thus, an abstract type cannot be in a pair.
    Eh ?? That would make a map containing base class pointers completely useless.

    Code:
    // Test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <map>
    #include <iostream>
    #include <conio.h>
    
    class IFoo
    {
        public:
            virtual ~IFoo() { }
            virtual void PrintOut() = 0;
    };
    
    class Foo : public IFoo
    {
        public:
            Foo() { }
            virtual ~Foo() { }
            virtual void PrintOut() 
            { 
                std::cout << "Hello from Foo" << std::endl;
            }
    };
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        typedef std::map<unsigned int, IFoo *> FooMap;
        
        FooMap testMap;
        std::pair<unsigned int,IFoo *> FooIntPair;
        FooIntPair.first = 0;
        FooIntPair.second = new Foo();
        testMap.insert(FooIntPair);
    
        FooMap::iterator iter;
        FooMap::iterator end(testMap.end());
    
        iter = testMap.find(0);
        if (iter != end)
        {
            Foo *pFoo = dynamic_cast<Foo *>(iter->second);
            pFoo->PrintOut();
        }
    
        iter = testMap.begin();
        while (iter != end)
        {
            delete iter->second;
            iter->second = 0;
            ++iter;
        }
        
        testMap.clear();
    
        _getch();
        return 0;
    }
    Technically dynamic_cast is not needed and static_cast will do since we know the object with key 0 is indeed a Foo.

    Another example using IFoo * as the key. Not sure why you would want to map one IFoo to another IFoo, but it is possible.
    Code:
    // Test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <map>
    #include <iostream>
    #include <conio.h>
    
    class IFoo
    {
        public:
            virtual ~IFoo() { }
            virtual void PrintOut() = 0;
    };
    
    class Foo : public IFoo
    {
        public:
            Foo() { }
            virtual ~Foo() { }
            virtual void PrintOut() 
            { 
                std::cout << "Hello from Foo" << std::endl;
            }
    };
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        
        typedef std::map<IFoo *, IFoo *> FooMap;
        FooMap testMap;
        
        std::pair<IFoo *,IFoo *> FooPair;
        
        Foo *pFooOne = new Foo();
        Foo *pFooTwo = new Foo();
    
        FooPair.first = pFooOne;
        FooPair.second = pFooTwo;
        testMap.insert(FooPair);
    
        FooMap::iterator iter;
        FooMap::iterator end(testMap.end());
    
        iter = testMap.find(pFooOne);
        if (iter != end)
        {
            
            Foo *pFoo = dynamic_cast<Foo *>(iter->second);
            pFoo->PrintOut();
            pFoo = dynamic_cast<Foo *>(iter->first);
            pFoo->PrintOut();
        }
    
        iter = testMap.begin();
        while (iter != end)
        {
            delete iter->first;
            delete iter->second;
            iter->second = 0;
            ++iter;
        }
        
        testMap.clear();
    
        _getch();
        return 0;
    }
    std::pair is just a template that allows you to specify the data types for first and second. You can put any data type in them. You could not do std::pair<IFoo,IFoo> b/c IFoo is abstract but you can certainly do std::pair<IFoo *,IFoo *> without ever creating an impl for IFoo. You will not be able to have first or second ever point at anything meaningful unless you create an impl for IFoo.

    I believe what brewbuck meant was that if you declare std::pair in a way that first or second are instances of objects then you must have instances of their type to use them in the pair. In this case you cannot have an instance b/c first or second is abstract. Keep in mind the pair declaration will always work provided the types exist but usage depends on the types you specified in the declaration.

    IE:
    std::pair<std::string,unsigned int> stringIntPair;
    Last edited by VirtualAce; 12-10-2009 at 05:19 PM.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    Thanks Bubba!!! That worked!!! Thank you very much!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. abstract classes 2
    By Aisthesis in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2009, 05:42 PM
  2. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. a simple C question...
    By DramaKing in forum C Programming
    Replies: 10
    Last Post: 07-28-2002, 02:04 PM
  5. API Reading Files, handle is always -1
    By Xei in forum C++ Programming
    Replies: 13
    Last Post: 05-06-2002, 10:16 PM

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