Running specific const/non-const method

This is a discussion on Running specific const/non-const method within the C++ Programming forums, part of the General Programming Boards category; Assume this class: Code: class B; class C { public: B* Get(); const B* Get() const; }; Is there a ...

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Running specific const/non-const method

    Assume this class:
    Code:
    class B;
    
    class C
    {
      public:
        B* Get();
        const B* Get() const;
    };
    Is there a way to specifically call one of those methods, like calling the non-const method though the const-version would work too? (normally const-methods are prefered over non-const)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Not really!
    Code:
    C c;
    
    B* b1 = c.Get(); //The first Get is called
    const B* b2 = c.Get(); //The second Get is called
    But assume I (for some weird unknown totally irrational reason) want the second Get to be called in the first example (b1)? That's not possible, or? (without casting to const B* which would work here but maybe not in the general case)

    Anyway, maybe my question is completely irrational and stupid.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can't use the second get for b1 because b1 is a non-const pointer and the second Get() returns a const pointer.

    I wouldn't be surprised if the first get was called in both situations. I think it usually depends on c, not b1 or b2.

    The best way to force the const version to be called is to create a const reference to c and call Get() from that. In the first example it won't work because b1 is not const, but in the second example it will force the const version of Get to be called, which is usually where this is an issue.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You can't use the second get for b1 because b1 is a non-const pointer and the second Get() returns a const pointer.
    I meant the other way around, use the first Get for b2 (hope it got right this time ).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    One way to do what you want is to declare a function ptr to each function. Then you can use the pointers to call whichever function you want.
    Code:
    #include <iostream>
    using namespace std;
    
    class B
    {
    };
    
    class C
    {
    public:
    	void get()
    	{
    		cout<<"non-const get() called"<<endl;
    	}
    	void get() const
    	{
    		cout<<"const get() called"<<endl;
    	}
    };
    
    
    int main()
    {
    
    	C nonConstMyC;
    	nonConstMyC.get(); //calls non-const get()
    
    	const C constMyC;
    	constMyC.get();  //calls const get()
    
    	cout<<endl;
    
    	cout<<"Non-const C object used to call both functions:"<<endl;
    	//Declare a pointer to the const get():
    	void (C::*ptrConstGet)() const = C::get; 
    	//Use the non-const C to call the const get():
    	(nonConstMyC.*ptrConstGet)();
    	
    	//Declare a pointer to the non-const get():
    	void (C::*ptrNonConstGet)() = C::get;
    	//Use the non-const C to call the non-const get():
    	(nonConstMyC.*ptrNonConstGet)();
    
    	return 0;
    }
    Actually, you only need to declare one function pointer: a pointer to the const get(). Then, you can call both functions like this:
    Code:
    nonConstMyC.get();
    (nonConstMyC.*ptrConstGet)()
    Last edited by 7stud; 02-03-2006 at 09:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  3. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 05:32 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 10:17 AM

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