Declaration of virtual class template list

This is a discussion on Declaration of virtual class template list within the C++ Programming forums, part of the General Programming Boards category; Hello C-mates, recently i rushed into the syntax of virtual funktion declaration and, unbelievabel, i declared some pure virtual funktions ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30

    Declaration of virtual class template list

    Hello C-mates,
    recently i rushed into the syntax of virtual funktion declaration and, unbelievabel, i declared
    some pure virtual funktions in a class. Besides, i declared some variables in that class too, unfortunately a list of objects of the same class... Of course the compiler complained and because of that mistake i now got a mess of unsable code.
    Though all could have worked, if the class could be derived and completed, the compiler does not accept my code.
    So is there a way to declare a "virtual" variable (in this case a list of virtual class objects) in a class? - I fear not, but if there are any ideas, i'm grateful for every help,
    Ben

    Sorry, but i forgot that it could be helpful to give an Example:
    Code:
    class isamess {
      virtual int tutnix() = 0;
      list<isamess> messies;
      .
      .
      .
    }
    Last edited by benshi; 02-02-2008 at 02:52 PM. Reason: giving an example

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm a bit confused about what you're trying to accomplish.
    What are you doing that you think using a "virtual" variable will help?
    What exactly would your definition of a virtual variable be?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Hi,
    i admit, that the task sounds a bit odd, but the idea is, that
    Code:
    list<isamess> messies;
    is replaced in the implementation of the class:
    Code:
    class tidyup{
    virtual int tutnix();  //definition now given somewhere
    list<tidyup> messies;
    .
    .
    }
    Now, messies are not "virtual" any longer.
    This would be convinient, because I have some functions in my abtract class already using
    the list of messies.
    Ben

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Huh? You can't change a definition of a variable to the implementation of a class.
    I'm still not sure what you're trying to accomplish. Pure virtual functions are simply virtual functions that makes the class abstract (meaning you can't create an instance of it).
    However, you can still derive from that class and all variables and functions that are protected and public will be visible and usable to the derived class.
    Virtual functions are for polymorphism, though. Do you use that?
    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.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    You can't do it that way because you are trying to use a class when it is not fully defined yet. Basically you are trying some sort of circular relationship in which one of tidyup members is a list of tidyup objects.

    Meanwhile the idea of a virtual function you have is not correct. It is not merely a means to define a member function elsewhere. Virtual member functions allow these functions definitions to be altered in a derived class. A virtual member function is the means by which, for instance you can define that while a dog and a wolf are both classes derived from the class canine, the sound() member function of the wolf emits a howl and that of the dog, a bark (if you allow me the simplicity of the example).
    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. #6
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Hi,
    thanks for your answer. I gave up an definend all member functions, despite of them being useless in the main class.
    But back to the problem, ill try to explain it again. The code given by:
    Code:
    class isamess {
      virtual int tutnix() = 0;
      list<isamess> messies;
      .
      .
      .
    }
    wont compile, as you sure will apreciate.
    The fact is, that there are functions in isamess, which use the variable "messies" and treat it like a normal list, as if the elements of this list were actually real class members.
    Of course this is not the truth, but it makes no difference, as all uses functions are actually supposed to exist, as soon, as a subclass, which implements them is defined.
    And due to the fact, that only instaces of such a subclass can be declared, the compiler would never be in the situation of not having the explicit functions given.
    The matter why i want to define such a class is, that i want to derive other classes from it, while the code itself is not useable and i want prevent it from being used.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Hi Mario,
    Obviously there is no problem in that circular relationship, as the list is not nescessarily filled with objects.

    i appreciate to what you have said, although i think, that this is not the only aim of a virtual function, as there is no need to define a virtual fuction in a base class (in this case it gets a pure virtual function). Declaring pure virtual functions is the fulfill of the idea to provide framework, which is in the very beginning not useable as real class but as a guide that defines some standards.
    Of couse souch a construction can always be replaced by a real object which can be changed afterwards, but the other way its a good possibility to arrange the code for a special goal.
    Last edited by benshi; 02-02-2008 at 04:01 PM. Reason: unclear arangement while talking about order

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If isamess is an abstract class, you can't have a list of them because an isamess object is not meant to be instantiated.

    Since you have virtual functions it might be more meaningful to store pointers to isamess objects. Although you cannot instantiate isamess itself, you might want to fill up the list with derived objects.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Hi anon,
    this seems to be a good solution to me, you really understood, what i wanted to archieve
    and you found my logical mistake.
    Thanks a lot to all of you,
    Ben

  10. #10
    Registered User
    Join Date
    Dec 2007
    Location
    Germany
    Posts
    30
    Hi again,
    the code now compiles flawlessly. Great thanks: you really showed me, how such a problem
    is structured.
    Ben

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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