Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all.

how to create get/set function to variables as below:
class A{
Private:
     char * m_sPathFile;
     char *m_fileName;
};


thanks
Posted
Comments
JackDingler 10-May-12 11:34am    
Is this a homework or an interview question?

Please note, the C++ keyword is private, all lowercase.
General speaking you provide the such accessors this way:
C++
class A
{
  // members are 'automatically' private by default
  int a; 

  // you explicitely declare the public section
public:
  // ..
  int get(){return a;}
  void set(int n){a=n;}
};

I chose a int data member on purpose, because direct handling of char pointers is a bit tricky (for instance do you need a copy of the string or what?) and probably you'll better use std::string instead.
 
Share this answer
 
If you are using Visual Studio the other way to do it is by using
__declspec and his property attribute. This will make you the setters and getters automatically

property (C++)
 
Share this answer
 
Comments
Schehaider_Aymen 10-May-12 7:37am    
Interesting
[no name] 14-May-12 5:54am    
Clever answer.
class A{
Private:
     string m_strPathFile;
     string m_strFileName;
Public:
void SetPathFile(string strPathFile);
string GetPathFile();

};

void A::SetPathFile(string strPathFile)
{
  m_sPathFile = strPathFile;
}

string A::GetPathFile()
{  
  return m_strPathFile;
}
 
Share this answer
 
Comments
ngthtra 15-May-12 6:31am    
If it is normal variable, i resolved but I can't when it is pointer variable.
in c++ Private spell as private. and there is no built in for get and set but you can create your own function to get and set value. just create public function that can receive char pointer for set and create a function that return pointer of character array for get.
 
Share this answer
 
Comments
ngthtra 11-May-12 3:39am    
if my variable is int or char , I can handle it, but this is pointer and I don't clear about pointer.Addition on, My code require, It can run on window and Linux.
C++
void A::setPathFile(char * value)
{
    m_sPathFile = new char[64];
    strcpy(m_sPathFilee, value);
}

char * Alarm_Sim_Request::getPathFile()
{
    return m_sPathFile;
}
 
Share this answer
 
Comments
Aescleal 15-May-12 7:04am    
Ask yourself the following... what happens if someone uses your class like this:

A a;

std::vector<char> path_data( 256, 'a' );
a.setPathFile( &path_data[ 0 ] );

You'll end up stomping all over memory you shouldn't and you'll probably end up crashing your program miles away from where the error happened.

So don't mess around with C style character arrays in C++ programs. Use a pair of strings the way infant_coder suggested (albeit in a pretty ill-described manner) and less people will be able to crash your code by accident or design.
Chuck O'Toole 15-May-12 8:05am    
Write this down in your little book of coding secrets:

Anytime you find yourself writing things like "new char[64]", any fixed string sizes, say to yourself "This will NEVER be bigger than 64!". Then say, mischieviously, "How can I make it bigger than 64?". And if it's easy to do that, then you have a bad design and you must fix it. Use dynamically sized strings or check lengths before you use the strings. Remember that Microsoft's biggest security flaws were because somebody thought "oh, they'll NEVER exceed my buffer's size".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900