Skip to content

1

:material-circle-edit-outline: 约 144 个字 :fontawesome-solid-code: 7 行代码 :material-clock-time-two-outline: 预计阅读时间 1 分钟

For the code below

void f() {
    Stash students();
 
}
  • Which statement is RIGHT for the line in function f()?
    • This is a variable definition, while students is an object of Stash, initialized w/ default ctor.
    • This is a function prototype, while students is a function returns an object of Stash.
    • This is a function call.
    • This is illegal in C++.

Correct Usages:

Function Prototype:

void f(Stash students); // Declares f taking a Stash argument

Variable Definition:

Stash students; // Creates a variable students of type Stash

Function Call (assuming students is a defined function):

students(); // Calls the function students

Copying

Create a new object from an existing one

Copying is implemented by the copy constructor

  • Has the unique signature T::T(const T&);

C++ builds a copy ctor for you if you don't provide one!

What if class contains pointers? Choices!

image-20240430140723547

Character strings

In C++, a character string is an array of characters with a special terminator — ‘\0’ or ASCII null

The string “C++” is represented, in memory, by an array of four characters

image-20240430141052991