C++ TR1

C++ TR1

Postby MC » Sun Mar 02, 2008 12:41 pm

User avatar
MC
 
Posts: 7
Joined: Sat Feb 17, 2007 12:58 am
Location: Singapore

Postby Cuchulainn » Sun Mar 02, 2008 7:21 pm

One of the really interesting new features of TR1 is shared_ptr (shared pointer) which provides some useful automation (memory safety) for native developers playing with pointers. shared_ptr is also performant as well as predictable. Good stuff.







I agree 100%. For novice and experienced C++ developers smart pointers are the way to go 8)
User avatar
Cuchulainn
 
Posts: 668
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sun Mar 02, 2008 7:26 pm

Here is some source code (I use a class Point, but you can use anything...)

///





// TestSharedPtr.cpp

//

// Testing shared pointers in boost

//

// 1. As private member data in classes

// 2. Storing shared pointers in STL collections

//

// (C)Datasim Education BV 2008

//



#include <vector>

#include "boost/shared_ptr.hpp"

#include "Point.hpp"



// These classes are an upgrade from raw pointers to

// shared pointers

class C1

{

private:

//double* d;

boost::shared_ptr<double> d;

public:

C1(boost::shared_ptr<double> value) : d(value) {}

virtual ~C1() { cout << "\nC1 destructor\n";}

void print() const { cout << "Value " << *d; }

};



class C2

{

private:

//double* d;

boost::shared_ptr<double> d;

public:

C2(boost::shared_ptr<double> value) : d(value) {}

virtual ~C2() { cout << "\nC2 destructor\n"; }

void print() const { cout << "Value " << *d; }

};



class D1

{

private:



boost::shared_ptr<Point> p;

public:

D1(boost::shared_ptr<Point> value) : p(value) {}

virtual ~D1() { cout << "\nD1 destructor\n";}

void print() const { cout << "\nValue " << *p; }

};



class D2

{

private:



boost::shared_ptr<Point> p;

public:

D2(boost::shared_ptr<Point> value) : p(value) {}

virtual ~D2() { cout << "\nD2 destructor\n";}

void print() const { cout << "\nValue " << *p; }

};



///

class Base

{ // Class with non-virtual destructor

private:



public:

Base() { }

virtual ~Base() { cout << "Base destructor\n\n"; }

virtual void print() const = 0;

};



class Derived : public Base

{ // Derived class

private:



public:

Derived() : Base() { }

~Derived() { cout << "Derived destructor\n"; }

void print() const { cout << "derived object\n";}

};



// Simple creator function

boost::shared_ptr<Base> createBase()

{

boost::shared_ptr<Base> result(new Derived());

return result;

}





int main()

{

{

cout << "Built-in types\n";

boost::shared_ptr<double> commonValue(new double (3.1415));



{

C1 object1(commonValue);

object1.print();

}



C2 object2(commonValue);

object2.print();

}



cout << "\nUser-defined types\n";



// Now with user-defined types

// Create dynamic memory

boost::shared_ptr <Point> myPoint (new Point(1.0, 23.3));

{

D1 point1(myPoint);

D1 point2(myPoint);

point1.print();



}



D2 object3(myPoint);

object3.print();



// Use in STL containers

typedef std::vector<boost::shared_ptr<Base> > ContainerType;

typedef ContainerType::iterator iterator;



// Create a vector of objects

ContainerType con;

for (int j = 0; j < 10; ++j)

{

con.push_back(createBase());

}



// Now iterate and print

iterator myIter;

for (myIter = con.begin(); myIter != con.end(); ++myIter)

{



(*myIter) -> print();

}



return 0;

}
User avatar
Cuchulainn
 
Posts: 668
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sun Jun 29, 2008 11:06 am

Video of the new developments in C++ (concurrency, concepts, etc.)



http://www.youtube.com/watch?v=ZAG5txfY ... re=related



concepts..



http://www.youtube.com/watch?v=Ot4WdHAG ... re=related



C++ programming styles (procedural, OO, Generic)



http://www.youtube.com/watch?v=zh8W4ZglOlw
User avatar
Cuchulainn
 
Posts: 668
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Re: C++ TR1

Postby Aliraza142 » Fri Nov 14, 2014 1:11 pm

MC

Could you tell us a bit more about this? Like using it in applications, if possible.

Many people would be interested.

Thanks
Aliraza142
 
Posts: 1
Joined: Fri Nov 14, 2014 12:40 pm


Return to C++

Who is online

Users browsing this forum: No registered users and 1 guest