Thread: Is There A Language That Can Do...

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Question Is There A Language That Can Do...

    Hello,

    It's late, I'm really tired but there's now something stuck in my head which is nagging me so I think I'd better share it and hopefully get some rest.

    I don't have much experience with C++, so I don't know if it does this explicitly. I also don't know what the name of this technique would be, so I can't search for it. Anyway...

    Suppose you had a base class like:-
    Code:
    // code might be wrong, again I don't usually do C++
    class CBaseClass
    {
         static CBaseClass *m_pClassPtr1;
         static CBaseClass *m_pClassPtr2;
         static CBaseClass *m_pClassPtr3;
    };
    So, the class contains three pointers which can point to instances of the same class.
    But what if I then wanted to derive a class from that:-
    Code:
    class CAnotherClass : public CBaseClass
    {
        // additonal member variables and methods
    };
    Instantiating CAnotherClass would give me an object with three pointers to instances of CBaseClass.

    What I would like to know is this: is there a way of specifying it so that m_pClassPtr1, 2 and 3 are always pointers to instances of the derived class, whatever that may be? If not in C++ in another language perhaps?

    It just crossed my mind that it should certainly be feasible...
    Last edited by SMurf; 09-23-2009 at 06:52 PM. Reason: Whoops, missed semicolon

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Maybe this:

    Code:
    template < typename D=void >
    class Base;
    
    template < typename D > class PointerTo { public: typedef D *Type; };
    template <> class PointerTo< void > { public: typedef Base< void > *Type; };
    
    template < typename D >
    class Base
    {
    protected:
        typename PointerTo< D >::Type ptr1; 
        typename PointerTo< D >::Type ptr2; 
        typename PointerTo< D >::Type ptr3; 
    
    };
    
    class Derived : public Base< Derived >
    {
    public:
        Derived()
        {
    	// Confirm that ptr1 is of type Derived *
    	Derived *p = ptr1;
        }
    };
    
    int main()
    {
        Derived d;
    }
    The PointerTo<> thing is to allow for Base<> to be used on its own without being derived from (if that's what you want).
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You mean like this?
    Code:
    template <typename T>
    class Base
    {
    protected:
    	static T* m_pClassPtr1;
    	static T* m_pClassPtr2;
    	static T* m_pClassPtr3;
    };
    
    class Derived : public Base<Derived>
    {
    };
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But then the classes no longer share a common base. Template instantiations are not related.

    The request doesn't make sense. These are static members. You say:
    Instantiating CAnotherClass would give me an object with three pointers to instances of CBaseClass.
    But this is just wrong. Your variables are static members; they exist once in the entire program. If you derive CYetAnotherClass from CBaseClass, which type should these three variables have?

    Did you perhaps not mean to make them static?

    However, even in this case, what you're asking is against the underlying idea of derivation as it is used in most object-oriented languages, because you try to change the interface of the base class in the derived class.

    The only thing you can do is make sure that the variables are only settable within the class itself. Then it can enforce the invariant that the pointers reference objects of the derived class. However, if it is possible for any client of the base class to set the pointers, enforcing this invariant would break the interface.
    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

  5. #5
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Well I picked up the static thing from an example I saw. Again, I don't write C++ so chances are the code that I've written won't make sense. Best to stick to the question. Sorry!

    So it can do it through templates then?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    But then the classes no longer share a common base. Template instantiations are not related.
    Then let Base<T> inhererit from SuperBase or something like that. But I missed the static part.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    So it can do it through templates then?
    Kind of, but it's really a solution looking for a problem.
    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

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CornedBee View Post
    Kind of, but it's really a solution looking for a problem.
    Yes, this is a C++ question.




    sorry, I had to do that.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 08-09-2009, 11:20 AM
  2. What language did they make Java in?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-03-2005, 04:18 PM
  3. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 02:40 PM
  4. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  5. Languages dying
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-29-2003, 10:08 AM