CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Programming Layout

  1. #1
    Join Date
    Jun 2006
    Posts
    29

    Question Programming Layout

    i am just starting c++, and would appreciate it if someone helped me understand some things.
    i have been coding with PHP for many years now, so i am certainly not new to programming, and know a respectable amount. But obviously PHP is very different from this.

    I want to start doing things, practicing, in directX. but i need some things answered about basic general c++ first.

    1) when you define a variable you can basically use int, String, bool, and a few others, but how can you define a variable where you don't really know what it's going to be, or it can be anything?

    2) say i wanted to do this:
    function returnvar(){
    return "test.txt";
    }
    function testfunction(){
    dosomething(returnvar())
    }
    i havn't figured out how to successfully do that yet

    3) probubly directx specific, but im not sure. i want to be able to have like classes in different CPP or H files that won't be used UNTIL i specify them to be used. i mean right now i can't figure out how to make a function that isn't just initiated on compile. heh. i want it to just be availible. i mean say i have a motion blur function, and onXevent, i want it to be initated. how would i setup a function like that?

    4) if i had something i wanted to have, example: motion blur, should i put that in a class or a function? and should that be in a CPP or an H file?

    5) how do you call to a class from another file and execute certain functions from it?
    Last edited by JustinMs66; September 26th, 2007 at 05:54 PM.

  2. #2
    Join Date
    Aug 2007
    Location
    Farnborough, Hants, UK
    Posts
    45

    Re: Programming Layout

    I'll be honest, you should probably Google for a basic C++ tutorial to get a good understanding, but I'll try and answer your questions just to get you started.

    1) This isn't really the way to do it. If a variable doesn't have a known type (loose typing), the compiler can't catch mistakes in coding because it can't check types passed around against the variables they will be going in. Powerful it may be, dangerous it is!

    You can, however, use the VARIANT structure (or COleVariant object, or better still, the _variant_t object). This can take any type, and in the case of _variant_t, has convenient C-style casts (extraction operators) to get values out in the desired format.

    2) Again, you're trying to create a function that can take any type as a parameter - you're heading for a world of pain.

    You can, however, create a templated function that can take any type of parameter. Since you're new to C++, though, that is going to hurt your head. Another way is to create 'overloaded' functions - in C++ you can write the same function (same name), but with different parameter types. This is a more manual way of doing what templated functions do.

    Thus, you can have

    void doSomething(int x)
    void doSomething(double y)
    void doSomething(MyObject& z)

    3) Again, this is just not the way it is done, but templated functions will, I believe, do something like this - the overload required for whatever parameter type you choose to pass won't be created until needed (don't quote me on that, my template knowledge is limited).

    4) If the concept you have in mind is an action (as blur is), then it should most likely be a function. If the concept is more like something you'd do an action on, such as an image, then it becomes a class.

    The actual code goes in the CPP file. Any information the compiler needs, to know how to use the code, or details about the class, go in the H file. If you let Visual Studio or your IDE create the classes for you, they create the H and CPP files automatically.

    5) There are two ways. The most likely way is that you create an actual instance of that class (a lump of memory that contains the data specified - an object), and call the methods on that instance.

    Pseudo code:

    In an H file:

    class MyClass
    {
    private:
    int x ;

    public:
    int getXTimes4();
    }

    In a CPP file:

    MyClass actualObject = new MyClass();

    int y = actualObject.getXTimes4();


    The other way is to use static methods where you don't need an instance, but as a starter in C++ you should probably concentrate on instantiating objects of a class until you get comfortable.
    Last edited by JTeagle; September 27th, 2007 at 03:25 AM.

  3. #3
    Join Date
    Jun 2006
    Posts
    29

    Red face Re: Programming Layout

    wow, you have no idea how much that helped! thank you so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)