-4

我最近使用c#并想继续在c++.

为什么?我喜欢游戏编程,我想DirectX在我的游戏中学习更好的图形

  • 什么时候使用'^'标志?(喜欢Dispatchertimer^ a=ref new Dispatchertimer();
  • new type()和有什么区别ref new type()?什么时候用哪一个?
  • 何时auto在变量或对象之前使用(如auto foo = ref new Foo();)?
  • 我们什么时候必须使用 ' ::'(like DispatcherTimer::Interval) 或 ' ->'(like a->interval) 或 ' .'(like Timespan.duration)?我可以使用 '.' 对于 c# 中的所有这些,但现在在 c++ 中有点令人困惑!
4

2 回答 2

1

编辑:这是指 C++ 而不是 C++-cli,帖子的标签现在表明问题所在。

C# 在某些方面基于 C++,但该语言的一个关键特性是 C++ 程序员必须处理的许多复杂问题由运行时为您处理<但解决方案是通用的,因此通常较慢。它使 C# 更容易学习,程序员也更容易从中获得出色的成果,而优秀的 C++ 程序员能够编写更小、更高效和更强大的应用程序。

在 C++ 中,通过在变量声明中的类型名称后放置一个“&”来指示引用。

int i = 0;
int& j = i; // j is now an un-counted ref to i

但是,这些不是 C# 的引用:它们没有被引用计数,也不能被重新分配。一旦您在上面将“j”别名为“i”,您就无法更改它引用的内容。

int i = 0;
int& j = i;
int k = 2;
j = k; // translates to i = 2

您还必须意识到,如果您的引用对象消失,您将引发未定义的行为:

int* i = new int(3); // "i" is actually a pointer, the address of the new integer.
int& j = *i;  // j references the integer to which i points.
j = 2;        // OK: translates to (*i) = 2;
delete i;     // return it to the pool
j = 3;        // UNDEFINED BEHAVIOR: translates to (*i) = 3 but we just released i.

C++ 中也没有“ref”关键字,因为没有内置的 ref 计数类型(但是,您可以使用 std::shared_ptr/std::weak_ptr 来实现类似的东西)。

“::”是作用域运算符,用来表示“属于作用域”,而“.” 和“->”分别表示“实例的成员”和“指向的实例的成员”。

当您想要访问类的常量/静态成员或命名空间的成员时,或者当您需要区分不同命名空间中的相似名称时,您将使用“::”。

class Foo {
public:
    void herp();
    enum { Bad = 0, Good = 1 };
    static int open(); // could class with C-standard library 'open' function.
};

// Implement the member "herp" of Foo
void Foo::herp()
{
}

由于 foo 的“open()”函数被声明为静态的,所以它不是由成员调用而是由类调用。

// Using "::"
    int quality = Foo::Good;
// Distinguish between 'open' in std vs foo
    int fd = std::open("helloworld.txt");
    int fd = Foo::open();

最后你问

What is difference between new type() and ref new type()

在 C++ 中,“new”的意思是“分配内存并返回一个指针”。此数据不会以任何方式进行引用计数或管理,您有责任确保释放内存,否则您将招致所谓的“内存泄漏”。考虑:

#include <iostream>

class Foo {
    char m_data[1024 * 1024 * 1024]; // 1Gb of data
public:
    Foo() {
        std::cout << "New Foo at " << (void*)this << std::endl;
    }
    ~Foo() {
        std::cout << "Releasing Foo at " << (void*)this << std::endl;
    }
    void doWork() {
        // imagine the foo is doing some work
    }
};

void makeFooDoWork() {
    static const size_t FOOS_AT_A_TIME = 16;
    Foo* f = new Foo[FOOS_AT_A_TIME];
    for (size_t i = 0; i < FOOS_AT_A_TIME; ++i) {
        foo[i]->doWork();
    }
    /* In C#:
       These objects would call Destroy() as they went out of
       scope making them available for garbage collection.
       In C++:
       allocated objects are YOUR responsibility. Nothing
       happens to them automatically.
     */
     // free [] f;  // <-- what we ought to do here.
}

int main(int argc, char* argv[]) {
    std::cout << "Creating first foo" << endl;
    Foo* f1 = new Foo;
    std::cout << "Releasing it" << endl;
    delete f1;

    for (size_t i = 0; i < 10000000; ++i) {
        makeFooDoWork();
    }
}

在可预见的未来,这个程序很可能会在大多数机器上崩溃,因为 C++ 没有垃圾收集,所以它完全不知道我们没有跟踪我们分配的对象。


由于看起来问题实际上是关于 C++/CLI,让我添加以下内容:

C++/CLI is a SECOND C++-derived language developed by Microsoft. You need to be careful in stipulating that you are asking questions about C++/CLI because it is NOT the same thing as C++, not by a long stretch. I cannot speak to the popularity of C++/CLI except that I don't know any C++/CLI programmers. I know Cobol programmers, C# developers, Objective-C guys out the wazoo, C programmers even ada and business basic programmers, I have a friend who's consultancy does nothing but LISP, I have dozens of friends who work for Microsoft and I have all kinds of microsoft-certified web/db/asp developers.

But nobody I know actually works with C++/CLI - I used it briefly to try and develop some .NET prototypes many years ago, but dropped it for C# the moment that was an option.

I would encourage you to choose either C++ or C#, and since you already know some C# that may be your best route (e.g. the Unity 3D engine lets you use C# for the scripting language); cross-platform development is possible with C# via Mono, C++/CLI is only, and barely, supported by Microsoft. C.f. There was no Intellisense for C++/CLI in VS2010, although it came back in 2012.

于 2013-09-29T19:51:36.377 回答
0
  • ^ is called a "hat" and it basically means a managed pointer, which points to an object that is garbage collected. You will find it only in .NET framework (C++/CLR), it's not C++ standard.
  • ref new type() is used when type is a reference class, again, that can be handled by a garbage collector. More about it here: http://www.directxtutorial.com/lessonarticle.aspx?id=0

  • auto keyword specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For example auto a = 3 will make a being of int type.

  • . and -> are both access operators, a.b means access member b of object a but a->b means access member b of object pointed by a. :: is a scope resolution operator.

But all those are basic things that you should have googled.

于 2013-09-29T19:56:09.450 回答