Initialization List.

Slides:



Advertisements
Similar presentations
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Plab – Exercise 5 C++: references, operator overloading, friends.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Variables and memory addresses
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Pointer to an Object Can define a pointer to an object:
Dynamic Storage Allocation
Overview 4 major memory segments Key differences from Java stack
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to C++ Systems Programming.
Motivation and Overview
Operator overloading Conversions friend inline
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
Chapter 7 CS 3370 chapter 7 CS 3370 – C++ Classes.
Operator overloading Conversions friend inline
Inheritance & Polymorphism
Pointers Revisited What is variable address, name, value?
Pointers and references
Pointers and References
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
Dynamic Memory Allocation
Object Oriented Programming COP3330 / CGS5409
Pass by Reference, const, readonly, struct
Introduction to Classes
Overview 4 major memory segments Key differences from Java stack
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
Constructors and Other Tools
Classes.
Pointers and References
Pointers Pointers point to memory locations
Overview of C++ Polymorphism
C++ Programming Lecture 17 Pointers – Part I
Pointers, Dynamic Data, and Reference Types
Pointers and references
Object Oriented Programming (OOP) Lecture No. 12
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Initialization List.
Workshop 1++: A bit of C++
Presentation transcript:

Initialization List

The paramaterless ctor (aka default ctor) class B {    A _a1,_a2; public:    B() {     std::cout << "B – parameterless " << "ctor\n"; } }; int main() {    B b; } class A { public:  A() {   std::cout << "A - " << "parameterless" << " ctor\n"; } }; // output A – parameterless ctor B - parameterless ctor

The paramaterless ctor (aka default ctor) class B {    A _a1,_a2; public:    B() {     std::cout << "B – parameterless " << "ctor\n"; } }; int main() {    B b; } class A { public:   A(int a) {    std::cout << "A ctor with one parameter\n"; } }; // compilation error No parameterless ctor for _a1, _a2

The initialization list int main() {    B b(2,3); } class B {    A _a1,_a2; public:   B(int i, int j) :_a1 (i), _a2(j) {    std::cout << "B cons"    << std::endl; } }; class A { public:    A(int a) {     std::cout << "A (" << a << ") "     << std::endl; } }; // output A (2) A (3) B cons

Initialization using pointers (1) int main() {    B b(2); } class B {    A *_ap; public:    B(int i); }; B::B(int i) {    _ap = new A (i);    cout << "B cons\n"; } class A { public:    A(int a) {     std::cout << "A (" << a << ") "     << std::endl; } }; // output A (2) B cons

Initialization using pointers (2) int main() {    B b(2); } class B {    A *_ap; public:    B(int i); }; B::B(int i) : _ap (new A(i)) {    cout << "B cons\n"; } class A { public:    A(int a) {     std::cout << "A (" << a << ") "     << std::endl; } }; // output A (2) B cons

The initialization list Initialization of object members. Initialization of constants and reference variables. In the future: Initialization of parent class. It is faster and safer to use the initialization list than initialization in the constructor

More on initialization & C++11: init_demo.zip

Reference variables

References A reference is an alias – an alternative name to an existing object

References, example (not a useful one) A reference is an alias – an alternative name to an existing object int i = 10; int& j = i; // j is a int reference // initialized only // once ! j += 5; // changes both i and j

References, example (not a useful one) A reference is an alias – an alternative name to an existing object int i = 10; int& j = i; // j is a int reference // initialized only // once ! j += 5; // changes both i and j int* k = new int(); j = k; // error k is a pointer j = *k; // ok j and i equals to *k

The famous swap More intuitive syntax No pointer arithmetic mistakes // C version void swap (int *a, int *b) {    int t = *a;    *a = *b;    *b = t; } // C++ version void swap (int &a, int &b) {    int t = a;    a = b;    b = t; } More intuitive syntax No pointer arithmetic mistakes Ref variables are actually const pointers (standard implementation) Must be initialized in their declaration (initialization list), like const variables

The famous swap: std::swap later in the course

Pointer vs non-const reference References can be used as output parameters, similar to pointers. Pros: It is hard to have reference to undefined value The syntax inside the function is clearer Cons: You can’t see what you are doing at call site (but this shouldn’t be a problem if the function is named right and documented)

Pointer vs non-const reference As a convention always order argument, in first out last.

Lvalue & Rvalue int a=1; a=5; // Lvalue = Rvalue, Ok a=a; // Lvalue = Lvalue, Ok 5=a; // Rvalue = Lvalue Comp. error 5=5; // Rvalue = Rvalue Comp. error Lvalues: variables, references … Rvalues: numbers, temporaries … Temporary: A result of expression that isn't stored – a+5 creates a temporary int with value 6.

R/L value and references non-const Reference – only to a non const Lvalue. const reference – to both Lvalue and Rvalue int lv=1; const int clv=2; int& lvr1=lv; int& lvr2=lv+1; //error! int& lvr3=clv; //error! const int& cr1=clv; const int& cr2=5+5;

R/L value and references non-const Reference – only to a non const Lvalue. const reference – to both Lvalue and Rvalue int lv=1; const int clv=2; int& lvr1=lv; int& lvr2=lv+1; //error! int& lvr3=clv; //error! const int& cr1=clv; const int& cr2=5+5; // This is useful for // Functions arguments

Lvalue & Rvalue Reference – only to Lvalue int a=1; a=5; // Lvalue = Rvalue, Ok a=a; // Lvalue = Lvalue, Ok 5=a; // Rvalue = Lvalue Comp. error 5=5; // Rvalue = Rvalue Comp. error Lvalues: varibales, refernces (non const!) … Rvalues: const variables refernces, numbers, temporaries … Temporary: A result of expression that isn't stored – a+5 creates a temporary int with value 6. Reference – only to Lvalue Const Reference – to Lvalue & Rvalue C++11: Rvalue reference (&& used for move ctor and assignments)

A fancy way to pass arguments to function // pass by const ref void foo (const int &a) {    ... } // Pass by value void foo (int a) {    ... } // Pass by pointer void foo (int *pa) {    ... } Avoid copying objects, without allowing changes in their value.

Return a reference to variable class Buffer {    size_t _length;    int *_buf; public:    Buffer (size_t l) :    _length (l),    _buf (new int [l])    {    }    int& get(size_t i)    {       return _buf[i];    } }; int main () {    Buffer buf(5);    buf.get(0)= 3; } Return a ref. to a legal variable (e.g. not on the function stack). Will be more useful with operators overloading

Ref to NULL? Possible to get, but undefined behavior. If you do: int* p= NULL; // nullptr c++-11 int& r= *p; You do get reference to NULL, but the behavior of such program is undefined.

Reference Must always refer to “legitimate” objects, i.e. allocated existing object After initialization, cannot refer to a different object The “interface” of references is as if they were the object themselves Often implicitly implemented by the compiler as pointers or helps to “inline” the function (we’ll soon learn what is “inline”)

By Value “By value” arguments cannot be changed! void swap(int a, int b) {    int temp = a;    a = b;    b = temp; } int main() {    int x=3, y=7;    swap(x, y);    // still x == 3, y == 7 ! } “By value” arguments cannot be changed!

Pointers vs. Reference void swap(int* a, int* b) {    int temp = *a;    *a = *b;    *b = temp; } ... int main() {    int x=3, y=7;    swap(&x, &y);    // x==7, y==3 } void swap(int& a, int& b) {    int temp = a;    a = b;    b = temp; } ... int main() {    int x=3, y=7;    swap(x, y);    // x==7, y==3 }

Summary But it can be viewed as always passing “by value”! The value can be pointer or reference! int * func(int *var0, int &var1, int var2); By pointer By reference By Value

References - why? Efficiency – avoid copying arguments Enables modifying variables outside a function But that can be done with pointers too! Everything that can be done with references, can be done with pointers But some “dangerous” features of pointers cannot be done (or harder to do) with references Easier to optimize by the compiler More convenient in many cases (see examples) Widely used as parameters and return values

Reference – more Like with pointers, don’t return a pointer or reference to a local variable You can return a pointer or a reference to a variable that will survive the function call, for example: A heap variable (malloc, new, etc.) A variable from a lower part of the stack Globals, static variables and static members of a class

void add(Point& a, Point b) {. // a is reference, b is a copy. a void add(Point& a, Point b) { // a is reference, b is a copy a._x+= b._x; a._y+= b._y; } int main() { Point p1(2,3), p2(4,5); add(p1,p2); // note: we don’t send pointers! // p1 is now (6,8) ... }

void add(Point& a, const Point& b) { // a is reference, // b is a const ref a._x+= b._x;    a._y+= b._y; } int main() {    Point p1(2,3), p2(4,5);    add(p1,p2); // note: we dont send pointers!     // p1 is now (6,8) ... } b is Reference => is not copied b is Const => we can't change it Important for large objects!

Point& add(Point& a, const Point& b) {    // a is reference, b is a const ref a._x+=b._x;    a._y+=b._y;    return a; } int main() {    Point p1(2,3), p2(4,5), p3(0,1);    add(add(p1,p2),p3); // now p1 is (6,9) cout << add(p1,p2).getX(); // note the syntax ... }

C++ const

Const variables – like in c int * const p1 = &i; // a const // pointer to an un-const variable p1++; // c.error (*p1)++; // ok const int * p2 = &b; // an un-const // pointer to a const variable p2++; // ok (*p2)++; // c.error const int * const p3 = &b; // a const // pointer to a const variable

Const objects & functions (1) class A { public:    void foo1() const;    void foo2(); }; void A::foo1() const { } void A::foo2() { } int main() {    A a;    const A ca;    a.foo1();    a.foo2();    ca.foo1();    ca.foo2(); // comp. // error }

Const objects & functions (2) class A { public:    void foo() const;    void foo(); }; void A::foo() const {    cout << "const foo\n"; } void A::foo() {    cout << "foo\n"; } int main() {    A a;    const A ca;    a.foo ();    ca.foo(); } // output foo const foo

Why? Overload resolution, again: A::foo(A* this) A::foo(const A* this)

Return a const ref. to variable class Buffer {    size_t _length;    int *_buf; public:    Buffer (size_t l):    _length (l), _buf (new int [l])    {    }    const int& get(size_t i) const    {       return _buf[i];    } }; int main () {    Buffer buf(5);    buf.get(0) = 3; // illegal std::cout << buf.get(0); } Why?

Const objects with pointers – like in c class B { public: int _n; }; class A { public: B* _p; A(); void foo() const; }; A::A() : _p(new B) { _p->_n = 17; } void A::foo() const { //_p++; // this will not // compile _p->_n++; // this will ! } int main() { const A a; std::cout << a._p->_n << std::endl; a.foo(); std::cout << a._p->_n << std::endl; } // output 17 18

Const objects with references class A { public:    int & _i;    A(int &i);    void foo() const; }; A::A(int &i) : _i(i) {     } void A::foo() const {    _i++; } int main() {    int i = 5;    const A a (i);    std::cout <<    a._i << std::endl;    a.foo();    std::cout <<    a._i << std::endl; } // output 5 6

Initialization of const and ref. class A {    int& _a;    const int _b; public:    A(int& a); }; A::A(int& a) {    _a = a;    _b = 5; } // compilation error Const and ref vars must initialized in their declaration (when they are created): For fields of a class it’s in the initialization list

Initialization of const and ref class A {    int& _a;    const int _b; public:    A(int& a); }; A::A(int& a) {    _a = a;    _b = 5; } class A {    int& _a;    const int _b; public:    A(int& a); }; A::A(int& a) : _a(a), _b(5) { } // compilation error // compiles ok

mutable mutable means that a variable can be changed by a const function (even if the object is const) Can be applied only to non-static and non-const data members of a class

mutable: example #1 class X { public: ... X() : _fooAccessCount(0) {} bool foo() const { ++_fooAccessCount; ... } unsigned int fooAccessCount() { return _fooAccessCount; } private: mutable unsigned int _fooAccessCount; };

mutable: example #2 class Shape { public: ... void set...(...) { _areaNeedUpdate= true; ... } double area() const { if (_areaNeedUpdate) { _areaNeedUpdate= false; } return _area; } private: mutable bool _areaNeedUpdate= true; mutable double _area; };