Advertisement

C++ Class Properties?

Started by July 14, 2000 02:50 AM
4 comments, last by RAGE4204 24 years, 5 months ago
Can you do this in C++? If so how? For Example (in Delphi):

type
  TSomeClass = class
  public
    property Name : string read GetName() write SetName();  
  private
    function  GetName() : string;
    procedure SetName(s : string);
  end;
 
-----------------------------------

procedure SomeProcedure();
var
  mySomeClass : TSomeClass;
begin
  mySomeClass := TSomeClass.Create;

  mySomeClass.Name := ''Some Class Name'';

  mySomeClass.Free;
end;
 
When "Some Class Name" is assigned to the TSomeClass.Name property it is passed through the private procedure TSomeClass.SetName(). When you read from TSomeClass.Name it gets the value from the private function TSomeClass.GetName(). Can anyone tell me how this is done in C++? Or if it is possible at all? Thanks.
class TSomeClass
{
public :
char Get_Name() ;
bool Set_Name(char Data[20]) ;
private :
char Name[20] ;
} ;

char TSomeClass::Get_Name()
{
return(Name) ;
}

bool TSomeClass::Set_Name(char Data[20]) ;
{
if(strcpy(Name, Data)
{
return(true) ;
}
else
{
return(false) ;
}
}
------------------------
void SomeProcedure()
{
// Local variables
//****************
TSomeClass mySomeClass ;

// Code
//*****
mySomeClass.Set_Name(Some Class Name) ;
// When you leave the function the class will delete itself
}
-------------------------
I think thats how you do it
I''m sure people will blast me if I''m wrong
Advertisement
What happened to all my lovely spacing ?
Defining properites is not possible in official ANSI C++ (as far as I know), but C++ Builder has some language extensions that make it possible. Pretty logical, since both Delphi and C++ Builder are Borland (or Inprise, whatever ) products. I can''t remember exactly how to do it, but it''s pretty much the same as in Delphi.

Twig Meister: if you want your code to have a nifty layout put it between [ source ] and [ /source ] (without the spaces). Like this:
    #include <iostream>using namespace std;int main() {  // Output some stuff  cout << "Blabla" << endl;  return 0;}    


Dormeur
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
This can also be done in Visual C++...

    _declspec(property(get=getfunc,put=putfunc)) ...    
Thanks everyone for the response, that was exactly what I was looking for Shinkage. Too bad it isn''t cross-platform/compiler

This topic is closed to new replies.

Advertisement