Which cast am I using?
Posted
by Knowing me knowing you
on Stack Overflow
See other posts from Stack Overflow
or by Knowing me knowing you
Published on 2010-04-14T18:40:11Z
Indexed on
2010/04/14
18:43 UTC
Read the original article
Hit count: 338
I'm trying to cast away const from an object but it doesn't work. But if I use old C-way of casting code compiles. So which casting I'm suppose to use to achieve this same effect? I wouldn't like to cast the old way.
//file IntSet.h
#include "stdafx.h"
#pragma once
/*Class representing set of integers*/
template<class T>
class IntSet
{
private:
T** myData_;
std::size_t mySize_;
std::size_t myIndex_;
public:
#pragma region ctor/dtor
explicit IntSet();
virtual ~IntSet();
#pragma endregion
#pragma region publicInterface
IntSet makeUnion(const IntSet&)const;
IntSet makeIntersection(const IntSet&)const;
IntSet makeSymmetricDifference(const IntSet&)const;
void insert(const T&);
#pragma endregion
};
//file IntSet_impl.h
#include "StdAfx.h"
#include "IntSet.h"
#pragma region ctor/dtor
template<class T>
IntSet<T>::IntSet():myData_(nullptr),
mySize_(0),
myIndex_(0)
{
}
IntSet<T>::~IntSet()
{
}
#pragma endregion
#pragma region publicInterface
template<class T>
void IntSet<T>::insert(const T& obj)
{
/*Check if we are initialized*/
if (mySize_ == 0)
{
mySize_ = 1;
myData_ = new T*[mySize_];
}
/*Check if we have place to insert obj in.*/
if (myIndex_ < mySize_)
{//IS IT SAFE TO INCREMENT myIndex while assigning?
myData_[myIndex_++] = &T(obj);//IF I DO IT THE OLD WAY IT WORKS
return;
}
/*We didn't have enough place...*/
T** tmp = new T*[mySize_];//for copying old to temporary basket
std::copy(&myData_[0],&myData_[mySize_],&tmp[0]);
}
#pragma endregion
Thanks.
© Stack Overflow or respective owner