English 中文(简体)
我使用什么东西?
原标题:Which cast am I using?
  • 时间:2010-04-14 18:40:11
  •  标签:
  • c++
  • casting

我试图从物体中抹去,但却没有工作。 但是,如果我使用旧的C-航道来编纂成文法。 因此,让Imppose用于实现同样的效果? 我不想 old。

//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

感谢。

最佳回答

有一个专用的C++投放操作员,负责处理座标:const_cast:

myData_[myIndex_++] = &const_cast<T>(obj);
问题回答

我认为,这是你回想的:

    myData_[myIndex_++] = &T(obj);//IF I DO IT THE OLD WAY IT WORKS

这并不是一个 cast语。 <代码>T(obj) 构造临时物体和& 处理非法。 你可以把一个点储存到一个临时点上,因为它在处决到下一个<代码>时便消失;。

这里的问题并不令人信服,而是你们的结构如何从根本上储存数据。 您是否希望提供一份结构拥有的可变拷贝? 或者,你是否想提及由他人管理的外部数据?

如果你想要该结构实际上包含它所交回的数据,那么你可以做这样的事情。

    myData_[myIndex_++] = new T(obj);

(But.15/ to delete subsequently.) 如果你想提及外部数据,但能够加以修改,你希望

void IntSet<T>::insert(T& obj) // remove const

如果你想用不可调和的字眼提及外部数据,那么

T const ** myData_;

将消除消除先天性的必要性。 (据指出,这将允许像<编码>inert(3)”这样的东西撞倒方案。)

However

只有当使用C++的内在工具时,你才会更好。 <>IntSet with MakeUnion,makeIntersection and makeSymmetDifference, 使用了分类std:vector<int> and/or std:set<int> with std:set_union/code>,std:_set_intersection/code>

问题不在于此。 如果你真的打算储存一个固定物体,那么就相应地宣布你的阵列,例如:

T const ** myData_;

如果由于你实际上需要稍后修改数据,那么你就应当提供论据副本或重新审查你的接口,因为现在你说,你没有改变这一论点。

你们是否希望这样做? 你们为什么会把一站起来,把愤怒传给别人,而不是陷入愤怒之中?

您很有可能储存一个临时点;const T& T。 例如,如果你有:

IntSet<int> int_set;
int_set.insert(1);
short foo= 2;
int_set.insert(foo);

这两项呼吁都要求<条码>IntSet<T>:inert <>/code>将产生一个点到一种临时价值,这几乎总是是一种ug。

以下职能有什么错误?

∗ foo(){t x = 3;tur &x;}

如果有? 当 f返回时,发生了什么? 我认为,在你开始采用模板和通用方案之前,你需要学习基本内容。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?