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;
}