add some casts experiments
This commit is contained in:
parent
b3818c58cb
commit
0b0c4cfdcb
3 changed files with 79 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -13,3 +13,4 @@ array-bounds
|
||||||
accumulate
|
accumulate
|
||||||
object-lifetime
|
object-lifetime
|
||||||
shared_ptr
|
shared_ptr
|
||||||
|
casts
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
cmake_minimum_required(VERSION 2.8)
|
cmake_minimum_required(VERSION 2.8)
|
||||||
|
|
||||||
# Build options
|
# Build options
|
||||||
set(CMAKE_CXX_COMPILER "clang++")
|
set(CMAKE_CXX_COMPILER "g++") # (g++ seems to actually do something with -Weffc++)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra -Weffc++")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra -Weffc++")
|
||||||
|
|
||||||
|
@ -13,3 +13,4 @@ add_executable(array-bounds array-bounds.cpp)
|
||||||
add_executable(accumulate accumulate.cpp)
|
add_executable(accumulate accumulate.cpp)
|
||||||
add_executable(object-lifetime object-lifetime.cpp)
|
add_executable(object-lifetime object-lifetime.cpp)
|
||||||
add_executable(shared_ptr shared_ptr.cpp)
|
add_executable(shared_ptr shared_ptr.cpp)
|
||||||
|
add_executable(casts casts.cpp)
|
||||||
|
|
76
casts.cpp
Normal file
76
casts.cpp
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class mybase {
|
||||||
|
protected:
|
||||||
|
|
||||||
|
std::string ids;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
mybase(std::string ids)
|
||||||
|
: ids(ids) {}
|
||||||
|
|
||||||
|
virtual void foo() {
|
||||||
|
std::cout << "i'm a mybase! ids: " << this->ids << std::endl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class myclass : public mybase {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
myclass(std::string ids)
|
||||||
|
: mybase(ids) {}
|
||||||
|
|
||||||
|
void foo() {
|
||||||
|
std::cout << "i'm a myclass! ids: " << this->ids << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
void nocast(myclass* x) {
|
||||||
|
x->foo();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ccast(void* x) {
|
||||||
|
// C-style cast, guess that's not the C++ way, but works.
|
||||||
|
myclass* foo = (myclass*) x;
|
||||||
|
foo->foo();
|
||||||
|
}
|
||||||
|
|
||||||
|
void staticcast(void* x) {
|
||||||
|
// C++ static_cast. When I *know* it's myclass*, and want to revert an implicit
|
||||||
|
// conversion.
|
||||||
|
myclass* foo = static_cast<myclass*>(x);
|
||||||
|
foo->foo();
|
||||||
|
}
|
||||||
|
|
||||||
|
void dynamiccast(mybase* x) {
|
||||||
|
// C++ dynamic_cast.
|
||||||
|
if (myclass* foo = dynamic_cast<myclass*>(x)) {
|
||||||
|
// only if x is actually a myclass*, this gets executed.
|
||||||
|
foo->foo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
myclass a("a");
|
||||||
|
mybase base("base");
|
||||||
|
|
||||||
|
std::cout << "== nocast" << std::endl;
|
||||||
|
nocast(&a);
|
||||||
|
// nocast(&base); <- Does not compile
|
||||||
|
|
||||||
|
std::cout << "== ccast" << std::endl;
|
||||||
|
ccast(&a);
|
||||||
|
ccast(&base);
|
||||||
|
|
||||||
|
std::cout << "== staticcast" << std::endl;
|
||||||
|
staticcast(&a);
|
||||||
|
staticcast(&base); // XXX actually works!
|
||||||
|
|
||||||
|
std::cout << "== dynamiccast" << std::endl;
|
||||||
|
dynamiccast(&a);
|
||||||
|
dynamiccast(&base);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue