Thread: class in class

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    class in class

    Well i have a call which has a set of pointers pointing to objects of the same type.... now some of the member functions in the class are generic and not specific to the object... Will the compiler optimize this or is memory wasted for in each instance of the object to create those member functions.. and any elegant way of optimizing this...

    my code

    Code:
    # include <string>
    # include <vector>
    # include <iostream>
    
    
    using namespace std;
    
    #define SIZE 5
    
    
    using namespace std;
    
    class telnet{
    		private:
    			string command;
    			class telnet *next[SIZE]; 
    			class telnet *parent;		 
    			int index;
    			int size;
    				
    		public:  
    			telnet();
    			~telnet();
    			void Tokenize(const string& str,vector<string>& tokens);
    			void add(vector<string>);
    			void addconvertor(string);
    			int find(string);
    			void display();
    	
    	
    	
    };	
    
    
    void::telnet::addconvertor(string str)
    {
    	vector <string> token;
    	Tokenize(str,token);
    	add(token);
    }
    	
    void telnet::display()
    {
    	cout<<"\n"<<command;
    	
    	for(int i=0;i<index;i++)
    	{
    		if(next[i]!=NULL)
    		next[i]->display();
    	}	
    }	
    
    telnet::telnet()
    {
    	index=0;
    	command="";
    	size=SIZE;
    
    	for(int i=0;i<size;i++)
    	next[i]=NULL;
    	
    	parent=NULL;
    }	
    
    telnet::~telnet()
    {
    	for(int i=0;i<index;i++)
    	{
    	 if(next[i]!=NULL)
    	 next[i]->~telnet();   
    	}	
    	
    delete(this);
    }
    	
    
    int telnet::find(string str)
    {
    	
    	for(int i=0;i<index;i++)
    	{
    		if(next[i]!=NULL && next[i]->command.compare(str)==0)
    		return i;
    	}	
    	
    	return -1;
    }	
    
     
    
    
    void telnet::add(vector<string> strs)
    {
    	if(!strs.empty())
    	{
    
    		if(command=="")
    		command=strs.at(0);
    
    		
    		strs.erase(strs.begin());
    		
    		int place=-1;
    		
    		if(!strs.empty())
    		place=find(strs.at(0));
    		
    		if(place==-1){
    			next[index]=new telnet;
    			next[index]->add(strs);			
    			index++;
    	  
    		}
    		else
    			next[place]->add(strs);
    
    		   
    						
    		
    	}	
    	
    }	
    
    
    void telnet::Tokenize(const string& str,vector<string>& tokens)
    {
    	 const string& delimiters = " ";
    	string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    	string::size_type pos	 = str.find_first_of(delimiters, lastPos);
    
    	while (string::npos != pos || string::npos != lastPos)
    	{
    
    		tokens.push_back(str.substr(lastPos, pos - lastPos));
    		lastPos = str.find_first_not_of(delimiters, pos);
    		pos = str.find_first_of(delimiters, lastPos);
    	}
    }
    
     
    
    int main()
    {
    	telnet t;
    	t.addconvertor("hi vasanth how are you man");
    	t.display();
    
    	
    	return 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Will the compiler optimize this or is memory wasted for in each instance of the object to create those member functions

    you only get a single copy of each function - all objects of that type share that copy whenever the function is invoked (unless the function is inline, of course).

    >> and any elegant way of optimizing this

    the code looks pretty lean as it is, unless it just isn't running fast enough for you there probably isn't much need for further optimization...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM