-4

I recently used c# and I want to continue programming in c++.

Why? I love game programming and I want to learn DirectX for better graphics in my games

  • When to use '^' sign? (like Dispatchertimer^ a=ref new Dispatchertimer();)
  • What is difference between new type() and ref new type()? When to use which one?
  • When to use auto before a variable or object when making it (like auto foo = ref new Foo();)?
  • when do we have to use '::'(like DispatcherTimer::Interval) or '->'(like a->interval) or '.'(like Timespan.duration)?I was able to use '.' for all of these in c# but now in c++ it is a little confusing!
SoapBox
  • 20,457
  • 3
  • 51
  • 87
Ali mohammadi
  • 49
  • 1
  • 5

2 Answers2

1

EDIT: This refers to C++ and not C++-cli which the post's tags now suggest the question is about.

C# is based on C++ in some ways, but a key feature of the language is that many of the complex issues C++ programmers have to deal with are handled for you by the run time< but the solutions are generic and so generally slower. It makes C# easier to learn and easier for a programmer to start achieving great results from, whereas a good C++ programmer has the ability to write much smaller, more efficient and more powerful applications.

In C++, references are indicated by placing an "&" after the type name in a variable declaration.

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

These are not C#'s references though: they are not ref-counted, and cannot be re-assigned. Once you have aliased "j" to "i" in the above, you cannot change what it references.

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

You also have to be aware that, if the object of your reference goes away, you elicit undefined behavior:

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.

There is also no "ref" keyword in C++, because there is no built-in ref counted type (you can, however, use std::shared_ptr/std::weak_ptr to achieve something similar).

"::" is the scope operator, used to indicate "belonging to the scope" whereas "." and "->" mean "members of an instance of" and "member of an instance pointed to by" respectively.

You'll use "::" when you want to access constant/static members of a class or members of a namespace, or when you need to disambiguate similar names that are in different namespaces.

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()
{
}

Since foo's "open()" function is declared static, it is not invoked by members but by Class.

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

Lastly, you asked

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

In C++ "new" means "allocate memory for and return a pointer to". This data is not ref-counted or managed in any way, it is your responsibility to ensure that the memory is freed otherwise you'll incur what is called a "memory leak". Consider:

#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();
    }
}

This program is likely to crash on most machines for the forseeable future because C++ does not have garbage collection so it is completely unaware that we are not tracking the objects we allocated.


Since it appears the question was actually about C++/CLI, let me add the following:

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.

kfsone
  • 23,617
  • 2
  • 42
  • 74
  • actually I want to learn c++ and DirectX to use in windows store apps at first and then I will try other things and I will find the best.do you think I really have to continue what I am doing or if not what do I have to do?what ever I do I want to use DirectX do you know what none-.net compiler I have to use with DirectX?I'm confused what to learn!thank you so much! – Ali mohammadi Sep 29 '13 at 20:23
  • [This answer](http://stackoverflow.com/questions/5902954/where-can-i-learn-directx-programming) might be helpful, it recommends XNA Game Studio, the expression edition of which is free http://msdn.microsoft.com/en-us/library/bb200104.aspx This would allow you to continue working in C#. If you want to develop in C++ you can grab Visual Studio Express for free. However, learning DirectX and C++ at the same time is going to be a mind-bender :) – kfsone Sep 29 '13 at 20:28
  • But I heard XNA isn't going to be supported in future by microsoft and when I try to install XNA 4.0 it show's the message of compatibility issue and when I continue installing it fails.no idea?win8 vs2012 – Ali mohammadi Sep 29 '13 at 20:41
  • Sorry, that I can't help with :( I've only done DX stuff with C++, otherwise I use C# for forms-related stuff and Unity3d – kfsone Sep 29 '13 at 20:43
  • Witch compiler is best to use for DX 11.1/C++ programming? – Ali mohammadi Oct 04 '13 at 08:34
  • That's probably worth it's own question, probably on the gaming-specific stack overflows: [GameDev](http://gamedev.stackexchange.com/) or [Arqade](http://gaming.stackexchange.com/) – kfsone Oct 04 '13 at 17:20
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.

tomi.lee.jones
  • 1,563
  • 1
  • 15
  • 22