visual c++ - Use assignment operator instead of calling a function in C++ -


i'm new c++, it's messing googlefu. i'm not sure ask guess.

i'm wondering if possible make can use assignment operator pass values objects.

the closest thing can think of i'm talking how python works when use @public , @myvar.setter. can say:

class myclass(object):      @public     def myvar(self):         return self._myvar      @myvar.setter     def myvar(self, value):         self._myvar = value;  myclass.myvar = 12  print myclass.myvar 

this print 12, never have call myvar method function called, "myvarset(12)" , "myvarget()".

the reason ask, because understand "string" not true data type, array of char. mean when include string, somehow has ability interesting assignment operator. instead of having assign variable following:

char word[5] = { "h","e","l","l","o" }; 

you can say:

string word = "hello"; 

what if 1 wanted able other types of arrays? let's wanted have array of integers , assign them like:

foo intarray = 13235; 

instead of saying:

foo intarray; intarray.setvar(.....) 

how that? possible?

all so-called assignments aren't assignments, initializations.

string word = "hello"; 

is copy initialization.

string word; word = "hello"; 

is initialization followed assignment.

and yes, it's entirely possible, need overload assignment operator:

class foo{    foo& operator = ( whatever ); }; 

however, doesn't seem want. if i'm not mistaken, you're looking way construct object using values, constructor for:

struct foo{     foo(int x) : _x(x) {}     int _x; }; 

and wouldn't write

foo foo; foo._x = 5; 

but directly

foo foo(5); 

to do

foo foo = foo(5); 

you'll need publicly visible copy constructor, (in case, default, compiler-generated 1 do). rule of 3 if you're managing resources.

this basic stuff, , trust me, it's narrowed down a lot here. you're best off learning c++ using book.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -