CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2008
    Posts
    12

    minor encapsulation issue

    Heya, I have a little issue.
    Code:
    #include	<string>
    //	class taggable
    //	lets me tag an instance of a class with a name that means something to me.
    class	taggable
    {public	:	//	nonconst methods
    	bool	set_tag( const std::string& new_tag )
    	{	this->tag = new_tag	;
    		return	true	;
    	}
    //private	:	//	internal data	//	this is commented out because I would prefer to use the private here, but am using public so the example will compile.
    public	:
    	std::string	tag	;
    }	;
    
    //	class	kiddy
    //	an example class that is a taggable
    class	kiddy	:	public taggable
    {
    }	;
    
    //	class	grade
    //	an example class that is a taggable
    class	grade	:	public taggable
    {
    }	;
    
    //	class something
    //	an example class that has a kiddy and has a grade in addition to being a taggable.
    class	something	:	public taggable
    {public	:	//	nonconst methods
    	//	set_tag
    	bool	set_tag( const std::string& new_tag )
    	{	this->tag = new_tag	;	//	this does not encapsulate the functionality of class taggable. Or at least I think "encapsulate" is the right term to talk about here, right?
    		this->something_useful.set_tag( new_tag+"'s kiddy" )	;
    		this->something_nifty.set_tag( new_tag+"'s grade" )	;
    		return	true	;
    	}
    private	:	//	internal data
    	kiddy	something_useful	;
    	grade	something_nifty	;
    }	;
    Here we have class taggable that I'm wanting to have many of my classes inherit from so I can have its functionality in all of them, namely the ability to name each of them so I can keep track of who is who and so on.

    My issue is that I want to be able to change the functionality of the set_tag function based on the class its in, but I don't have any particular desire to know how the taggable class is implemented. I've run into the specific scenario where when an object that is taggable "has an" object that is also taggable, I would like to be able to write a new set_tag function for that object that will change the name of that object and its possessions.

    Unfortunately, I don't think making set_tag virtual in class taggable and writing a new set_tag for any dervied object with strange needs is what I want since I explicitly don't want to have to know how class taggable actually goes about setting its own tags.

    What would solve this problem I think would be a way to explicitly call the set_tag function in taggable from the set_tag function in class something, but I don't know how to do this. Does anyone have any ideas?

  2. #2
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: minor encapsulation issue

    You can call the base class version of a function (taggable::set_tag) from the derived class version (something::set_tag) by qualifying it with the base class name explicitly:

    Code:
    #include <string>
    
    // class taggable
    // lets me tag an instance of a class with a name that means something to me.
    class taggable
    {
    public:	//	nonconst methods
    	bool set_tag(const std::string& new_tag)
    	{	
            this->tag = new_tag;
    		return true;
    	}
    private	:	//	internal data	// NOW COMPILES FINE
    std::string	tag	;
    };
    
    // class	kiddy
    // an example class that is a taggable
    class kiddy	: public taggable
    {
    };
    
    // class	grade
    // an example class that is a taggable
    class grade	: public taggable
    {
    };
    
    // class something
    // an example class that has a kiddy and has a grade in addition to being a taggable.
    class something	: public taggable
    {
    public:	//	nonconst methods
    	//	set_tag
    	bool set_tag(const std::string& new_tag)
    	{	
            taggable::set_tag(new_tag);  // CALL BASE CLASS VERSION
            this->something_useful.set_tag( new_tag+"'s kiddy" )	;
    		this->something_nifty.set_tag( new_tag+"'s grade" )	;
    		return	true	;
    	}
    private	:	//	internal data
    	kiddy something_useful	;
    	grade something_nifty	;
    };
    Old Unix programmers never die, they just mv to /dev/null

  3. #3
    Join Date
    Apr 2008
    Posts
    12

    Re: minor encapsulation issue

    That's nifty & works, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured