Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

Similar documents
CSI33 Data Structures

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Elevate your Code to Modern C++ with Automated Tooling. Peter Sommerlad

Separate Compilation of Multi-File Programs

EL2310 Scientific Programming

Lecture on pointers, references, and arrays and vectors

GCC : From 2.95 to 3.2

Function Templates. Consider the following function:

C++ Scope Resolution Operator ::

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Programmazione. Prof. Marco Bertini

15. Pointers, Algorithms, Iterators and Containers II

Introduction to C++ Introduction to C++ 1

CS197c: Programming in C++

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

C++ for Python Programmers

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Lab 1: First Steps in C++ - Eclipse

Class Destructors constant member functions

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Classes and Objects in C++

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Comp151. Generic Programming: Container Classes

The Compilation Process

STL Quick Reference for CS 241

CPSC 427a: Object-Oriented Programming

Functions. Functions in C++ Calling a function? What you should know? Function return types. Parameter Type-Checking. Defining a function

Exercise 1.1 Hello world

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Namespaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

14. Pointers, Algorithms, Iterators and Containers II

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

The Standard Template Library. An introduction

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

G52CPP C++ Programming Lecture 15

Working with Batches of Data

COMP6771 Advanced C++ Programming

Lambda functions. Zoltán Porkoláb: C++11/14 1

use static size for this buffer

OBJECT ORIENTED PROGRAMMING USING C++

Understanding main() function Input/Output Streams

Fast Introduction to Object Oriented Programming and C++

EL2310 Scientific Programming

G52CPP C++ Programming Lecture 18

y

/ binary_search example #include <iostream> // std::cout #include <algorithm> // std::binary_search, std::sort #include <vector> // std::vector

Your first C and C++ programs

4. Structure of a C++ program

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Biostatistics 615/815 Lecture 6: Linear Sorting Algorithms and Elementary Data Structures

Exercise 6.2 A generic container class

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Introduction to C++ Systems Programming

CS93SI Handout 04 Spring 2006 Apr Review Answers

CSE 303: Concepts and Tools for Software Development

C++ Exception Handling 1

Object Oriented Design

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Computational Physics

Due Date: See Blackboard

Engineering Tools III: OOP in C++

Tutorial-2a: First steps with C++ programming

Exceptions, Case Study-Exception handling in C++.

Global & Local Identifiers

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

EL2310 Scientific Programming

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Lesson 13 - Vectors Dynamic Data Storage

I m sure you have been annoyed at least once by having to type out types like this:

Introducing C++ to Java Programmers

Computer Programming

Computer Science II Lecture 1 Introduction and Background

Reliable C++ development - session 1: From C to C++ (and some C++ features)

BITG 1113: Function (Part 2) LECTURE 5

C Functions. Object created and destroyed within its block auto: default for local variables

C++ Programming for Non-C Programmers. Supplement

Informatik I (D-ITET) Übungsstunde 9, Hossein Shafagh

Templates Templates are functions or classes that are parameterized. We have already seen a few in STL:

04-19 Discussion Notes

Programming C++ Lecture 2. Howest, Fall 2014 Instructor: Dr. Jennifer B. Sartor

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Functions and Recursion

Computer Science II Lecture 2 Strings, Vectors and Recursion

CS 376b Computer Vision

CS

Template based set of collection classes STL collection types (container types)

PHY4321 Summary Notes

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

10. Functions (Part 2)

COMP322 - Introduction to C++

Transcription:

COMP151 Namespaces

Motivation [comp151] 1 Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide: // File: gnutils.h class Stack {... ; class Some Class {... ; void gnome(); int func(int); // File: msutils.h class Stack {... ; class Other Class {... ; void windows200(); int func(int);

Motivation... [comp151] 2 Even if you do not use Stack and func, you run into trouble: the compiler will complain about multiple definitions of Stack; the linker will complain about multiple definitions of func. #include gnutils.h #include msutils.h int main() { Some Class sc; Other Class oc;... if (choice == LINUX) gnome(); else if (choice == MSWINDOWS) windows2000();

Solution: namespace [comp151] 3 If the library writers would have used namespace, multiple names would not be a problem. // File: gnutils.h namespace gnu { class Stack {... ; class Some Class {... ; void gnome(); int func(int); // File: msutils.h namespace microsoft { class Stack {... ; class Other Class {... ; void windows2000(); int func(int);

Namespace Alias & Scope Operator :: [comp151] 4 You refer to names in a namespace with the scope resolution operator. #include gnutils.h #include msutils.h namespace ms = microsoft; // namespace alias int main() { gnu::some Class sc; gnu::stack gnu stack; ms::other Class oc; ms::stack ms stack; int i = ms::func(42); if (choice == LINUX) gnu::gnome(); else if (choice == MSWINDOWS) ms::windows2000();

using Declaration [comp151] 5 If you get tired of specifying the namespace every time you use a name, you can use a using declaration. #include gnutils.h #include msutils.h namespace ms = microsoft; using gnu::some Class; using gnu::stack; using ms::other Class; using ms::func; int main() { Some Class sc; Other Class oc; Stack gnu stack; ms::stack ms stack; int i = func(42); // namespace alias // Refer to gnu::some Class // Refer to ms::other Class // Refer to gnu::stack // Refer to ms::func

Ambiguity with using Declarations [comp151] 6 You can also bring all the names of a namespace into your program at once, but make sure it will not cause any ambiguity. #include gnutils.h #include msutils.h namespace ms = microsoft; using namespace gnu; using namespace ms; int main() { Some Class sc; Other Class oc; // namespace alias // Refer to gnu::some Class // Refer to ms::other Class Stack S; ms::stack ms stack; gnu::stack gnu stack; // Error: ambiguous // OK // OK

namespace std [comp151] 7 Functions and classes of the standard library (string, cout, isalpha(),... ) and the STL (vector, list, for each, swap,... ) are all defined in namespace std. Although the following works, it is considered a bad practice. Here, we bring all the names that are declared in the three header files into the global namespace.

Example: namespace std [comp151] 8 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v; vector< int >::iterator it; v.push back(63); it = find(v.begin(), v.end(), 42); if (it!= v.end()) cout "Found 42!" endl; //... push back some more ints

Explicit Use of using Declaration [comp151] 9 It is better to introduce only the names you really need, or to qualify the names whenever you use them. #include <iostream> #include <vector> #include <algorithm> using std::vector; using std::find; using std::cout; using std::endl; int main() { vector<int> v; vector<int>::iterator it; v.push back(63); //... push back some more ints it = find(v.begin(), v.end(), 42); if (it!= v.end()) cout "Found 42!" endl;

[comp151] 10 Explicit Use of namespace Per Object/Function #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v; std::vector<int>::iterator it; v.push back(63); //... push back some more ints it = std::find(v.begin(), v.end(), 42); if (it!= v.end()) std::cout "Found 42!" std::endl; Although this takes more typing effort, it is also immediately clear which functions and classes are from the standard (template) library, and which are your own.

Final Remarks [comp151] 11 A combination of using declarations and explicit scope resolution is also possible; this is mostly a matter of taste. In g++, the classes and functions of the standard library and the STL are not defined in namespace std, but in the global namespace. That s why you get away with forgetting using declarations. However, this will most likely change in the future, so you better get used to it. VC++ already does it the right way.