Confusion in C ++

I am very new to C ++ and now I am learning it. I have a few questions.

  • What is the difference between void DoSomething (const Foo & foo) and void DoSomething (Foo foo)? If we do not specify, then the Foo instance will be passed by value (without reference). It will be the same as const + and in the argument, except for verification at compile time. So, why does it have const + and become the best practice for argument without and and const?

    In C #, passing an object is a reference, but it looks like it's not in C ++.

  • The book I'm reading says that member functions pass an implicit parameter by reference.

    Can someone give me a sample of an implicit parameter and by reference? I know that if we want to pass an object by reference, we need to use and (for example, Foo (Person & p)), but why does C ++ pass the object by reference for an implicit parameter? I read that the implicit parameter in C ++ is similar to Contructor (string str): strMemberVariable (str) {} ...

  • Is an array the only one that follows a link in C ++?

  • Why can't I use Foo fInstance in the Foo class?

Example:

class Foo {

public:    
    Foo() { }

    Foo(const Foo& f) : fInstance(f) {   }  

    Foo fInstance;      
};

Thanks in advance.

+5
source share
12 answers

1 void DoSomething (const Foo & foo) void DoSomething(Foo foo)? , Foo ( ). , const + , . , const + and const?

# " ", , ++.

:

  • Foo ,
  • Foo , ,
  • , const
  • , , , , ...

2 , , , - .

- ? , , (, Foo (Person & p)), ++ ? , ++ Contructor (string str): strMemberVariable (str) {}...

this, . , -.

Konrad: , this , this () , . ;)

3 , ++?

. , () .

FredOverflow, :

void fun(int* p, size_t size);

int main(int argc, char* argv[])
{
  int array[15];
  fun(array, 15);
}

, fun, , , array, array 15 : , .

array :

void changer(int*& array, size_t& size);

, ( ). , , , .

4 Foo fInstance Foo?

. Foo. Foo , , , . , 1, . , Foo Foo, :)?

:

class Foo
{
public:

private:
  std::unique_ptr<Foo> mInstance;
};

, , :)

+10

, :

void DoSomething(const Foo& foo) void DoSomething(Foo foo)?

, ( Foo).

, const + and const?

, (, ). , . : . , - . , - , .

, const, () . .

, , , - .

, . - this, . , ++ . , .

, ++?

++ - :

void foo(int[] x) { … }

void foo(int* x) { … }

. x, ++ - "". , foo(x) foo(&x[0]).

, :

void foo(int (&x)[4]);

, .

+7

# " ", , ++.

, , . , #, VB Java, ( ref # ByRef VB).

++ , , . , , , , ( ).

. # , :

void foo(string s) {
    s = "world";
}

string s = "hello";
foo(s);
Console.WriteLine(s); // prints "hello"
+4

Foo fInstance Foo?

Foo . Foo Foo.

, , , , Foo .

+4

void DoSomething(const Foo& foo) void DoSomething(Foo foo) - , . :

  • . . , .
  • . . , .
  • . , . , .

# 3, :

std::string global_string;

void foo(const std::string &str)
{
    if (str.empty())
    {
        global_string = "whatever";
        // is str still empty??
    }
}

foo foo(global_string), global_string str.

+1

:

  • doStuff(Foo f) , Foo - AKA. doStuff(const Foo &f) , , , . , . Java/#.

  • ?

  • , ( , std::array s) , - . , C- ( ) .

+1

, , , - .

this, -, . , ++ - , , .

class FOO{
 int x;
 void doSomthing(int x);
}

void FOO::doSomething(int x){
  x = x;
}

void FOO::doSomething(FOO* this, int x){
  this->x = x;
}

, , , , , .

0

" " const .

, .

, , , , ( , , std::for_each )

const , . , . , , , .

, . .

(const ) . , B D. D const B&, B, ( B, D),.

, , , , . - .

0
  What is the differences between void DoSomething(const Foo& foo) and

void DoSomething (Foo foo)?

, const "foo", , const Foo & foo , , .

0

void DoSomething (const Foo & foo) void DoSomething (Foo foo)?

, ( , Foo). ( , Foo).

Foo. Foo . , , , , , .

- ? , , (, Foo (Person & p)), ++ ? , ++ Contructor (string str): strMemberVariable (str) {}...

, (, ), ( ) .

class Foo
{
    Foo(int x) {...}
};

. :

Foo foo = 123;

void f(const Foo& x);
f(123);

:

class Foo
{
    explicit Foo(int x) {...}
};

... . :

Foo foo(123);

void f(const Foo& x);
f(Foo(123) );

, , , , .

, ++?

, , , , . / :

// takes an array of 10 integers
void fn(int(&some_array)[10]);

// takes a pointer to an int array
void fn(int* some_array);

// takes a pointer to an int array (the 10 
// literal constant is ignored) and this function
// can likewise take any pointer to int
void fn(int some_array[10]);

Foo fInstance Foo ?

. Foo fInstance, fInstance fInstance .. , , , , , , . , , . Foo - .

0
void DoSomething(Foo foo)

foo

void DoSomething(Foo& foo)

foo, , foo , foo. , .

, , ( ).

array[5] = 0;//is the same as :
*(array+5) = 0; //this
-1

void DoSomething (const Foo & foo) void DoSomething (Foo foo)

DoSomething (Foo foo) foo , Foo , , Foo . , foo, , . DoSomething (const Foo & foo), foo ( , ) foo DoSomething. , .

- ?

An example of an implicit parameter in member functions is a reference to the parent object, i.e. this, which is never mentioned in a function definition, but is always available for use.

Is an array the only reference passed in C ++?

No, all user objects are passed by reference.

-5
source

All Articles