Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ variable assignment, is this a normal way..?

This may be a silly question, but still I'm a bit curious... Recently I was working on one of my former colleague projects, and I've noticed that he really loved to use something like this:

int foo(7);

instead of:

int foo = 7;

Is this a normal/good way to do in C++ language? Is there some kind of benefits to it? (Or is this just some silly programming style that he was into..?)

This really reminds me a bit of a good way how class member variables can be assigned in the class constructor... something like this:

class MyClass
{
public:
   MyClass(int foo) : mFoo(foo)
   { }

private:
   int   mFoo;
};

instead of this:

class MyClass
{
public:
   MyClass(int foo)
   {
      mFoo = foo; 
   }

private:
   int   mFoo;
};
like image 215
Gediminas Avatar asked Sep 05 '12 15:09

Gediminas


People also ask

How does variable assignment work in C?

To assign a value to a C and C++ variable, you use an assignment expression. Assignment expressions assign a value to the left operand. The left operand must be a modifiable lvalue. An lvalue is an expression representing a data object that can be examined and altered.

What does variable assignment mean?

Variable Assignment To "assign" a variable means to symbolically associate a specific piece of information with a name. Any operations that are applied to this "name" (or variable) must hold true for any possible values.

What is the difference between an assignment and an initialization of a variable?

What is the difference between initialization and assignment? Initialization gives a variable an initial value at the point when it is created. Assignment gives a variable a value at some point after the variable is created.

What do we call an assignment made when you declare a variable?

Variable assignment An assignment statement consists of a variable name, the assignment operator = , and a value to assign to the variable. You must declare a variable before you can assign a value to it; make sure your variable declaration statement precedes any assignment statement.


2 Answers

For basic types there's no difference. Use whichever is consistent with the existing code and looks more natural to you.

Otherwise,

A a(x);

performs direct initialization, and

A a = x;

performs copy initialization.

The second part is a member initializer list, there's a bunch of Q&As about it on StackOverflow.

like image 120
Luchian Grigore Avatar answered Oct 24 '22 23:10

Luchian Grigore


Both are valid. For builtin types they do the same thing; for class types there is a subtle difference.

MyClass m(7);  // uses MyClass(int)
MyClass n = 3; // uses MyClass(int) to create a temporary object,
               // then uses MyClass(const MyClass&) to copy the
               // temporary object into n

The obvious implication is that if MyClass has no copy constructor, or it has one but it isn't accessible, the attempted construction fails. If the construction would succeed, the compiler is allowed to skip the copy constructor and use MyClass(int) directly.

like image 33
Pete Becker Avatar answered Oct 24 '22 22:10

Pete Becker