Archived

This topic is now archived and is closed to further replies.

C++ Class Properties?

This topic is 6362 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites