This code snippet makes no sense...  
Author Message
Bapa





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

I've come across some code syntax I've never seen. It compiled, but I don't know how, or what it even does!

I've been reading a book about DirectX, and in one of the examples, this little piece of code was present:

D3DXMATRIX *D3DXMatrixIdentity(D3DXMATRIX *pout);

"D3DXMATRIX" is a type, of course. And "D3DXMatrixIdentity" is a function, of course. Now I don't see how this works.

Okay, to break it down, first we have a type, which is D3DXMATRIX, then we have the indirection-operator (which I assume is a pointer), and then we have a function (perhaps a call) with a new D3DXMATRIX (pointer) as it's only parameter.

How the heck does this work It isn't just a regular function-call. It isn't a function-call that'll return anything to something, so whaaaa

To further illustrate my confusion, I'll give another example:

class someClass

{

};

someClass someFunction(someClass xx)

{

return xx;

}

int main()

{

someClass blah;

someClass *someFunction(&blah);

That code makes no sense at all to me. And if you change the return-type, or if you pass in a variable by value, rather than by it's address, I get a compile error.

And if anyone can explain how this works, can you please tell me what it actually does

I'm sure I can figure this out myself, and there's bound to be some documtation on this somewhere, but it's kinda hard to describe this problem :P

Like I said, I'm sure I can figure it out, but asking to see someone knows is good too.

Thanks :)





Visual C++4  
 
 
Simple Samples





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

 
D3DXMATRIX *D3DXMatrixIdentity(D3DXMATRIX *pout);

That type of function is very common.

 
"D3DXMATRIX" is a type, of course. And "D3DXMatrixIdentity" is a function, of course. Now I don't see how this works.
Not of course. The type is not "D3DXMATRIX"; it is "D3DXMATRIX *". You might need to use a tutorial or book about the C++ language to understand what a "D3DXMATRIX *" is but it is a pointer. The function returns a pointer.

 
Okay, to break it down, first we have a type, which is D3DXMATRIX, then we have the indirection-operator (which I assume is a pointer), and then we have a function (perhaps a call) with a new D3DXMATRIX  (pointer) as it's only parameter.

How the heck does this work It isn't just a regular function-call. It isn't a function-call that'll return anything to something, so whaaaa

It is very common and the only reason it seems unusual is that you are unfamiliar with C++.

 
And if anyone can explain how this works, can you please tell me what it actually does
There are abundant resources available that explain the fundamentals of the language very well. Most volunteers here will not attempt to duplicate the material that is so amply available.



 
 
Viorel.





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

I think the first is a declaration of D3DXMatrixIdentity function, which means "somewhere in this or other file there is a function called D3DXMatrixIdentity which takes a pointer to a D3DXMATRIX object and return same kind of pointer". It just allows the compiler to interpret your program.

The second has a different meaning: "from now someFunction is a pointer to a someClass object. It is initialized with the address of blah local variable". Note that the name of this pointer is not very suitable, since it overrides a function with the same name.

I hope this makes sense.


 
 
Simple Samples





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

someClass *someFunction(&blah);

I don't know what that is, except I think it is not valid code. You should create a small program you can use to test things like that and then post only valid code.

Note that the parameter is "&blah", which is a pointer to blah. In the class, the parameter for someFunction is not a pointer. Something that would be valid would be:

someClass blahblah = someFunction(blah);

In reality, you are unlikely to do that but that is best explained in a tutorial or book.



 
 
einaros





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

someClass *someFunction(&blah);

I don't know what that is, except I think it is not valid code. You should create a small program you can use to test things like that and then post only valid code.

If someClass is a defined class, there's nothing wrong with that syntax. It'll simply declare a variable someFunction, of type pointer-to-someClass, which is initialized with the address of blah.

The problem, however, is that it will redeclare the symbol that is someFuction, and thus making the gloabal / namespace scope someFunction (which happens to actually be a function) unreachable.



 
 
Simple Samples





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

I at least was not too far off by suspecting that code. When I compile the code shown below using VC 6 I get "error C2059: syntax error : '&'" for the line "someClass *someFunction(&blah);". When I compile using VC 2005, I get "warning C4189: 'someFunction' : local variable is initialized but not referenced".

class someClass {
};

someClass someFunction(someClass xx) {
return xx;
}

int main(int argc, char* argv[]) {
(void)argc, argv;
someClass blah;
someClass *someFunction(&blah);
return 0;
}



 
 
Ben Anderson MSFT





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

As stated, you are looking at a declaration of a function which returns a pointer to a D3DXMATRIX. Your code won't compile becaus you have the declaration inside a function. If you put it at global scope, it would be a forward declaration of a function which you would define later.

 
 
einaros





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

When I compile the code shown below using VC 6 I get "error C2059: syntax error : '&'" for the line "someClass *someFunction(&blah);".

That says more about VC6, than the construct as such, though



 
 
Simple Samples





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

That says more about VC6, than the construct as such, though
I think both. I do know that previous versions of the compiler had major incompatibilies with the standard. You know the language better than I, so I admit you could be totally correct. I think however that if the syntax were common, then it would have been compiled more correctly by earlier versions.

 
 
Simple Samples





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

I think that my first response was marked as an answer but it seems to have been unmarked. I assume that it was unmarked by the person that asked the question. I am not complaining about that except I hope most people agree that tutorials, books and other resources would be beneficial here. I assume that Bapa objected to that part.

 
 
einaros





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

I think that my first response was marked as an answer but it seems to have been unmarked. I assume that it was unmarked by the person that asked the question. I am not complaining about that except I hope most people agree that tutorials, books and other resources would be beneficial here. I assume that Bapa objected to that part.

That may very well have been me. I was marking your post as well as Viorel's. If yours was already marked, my click would have unmarked it instead. Corrections applied



 
 
einaros





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

 

I think however that if the syntax were common, then it would have been compiled more correctly by earlier versions.

If you're ever in doubt about such issues, and cannot be bothered to check the standard (or it's difficult to locate an un-ambiguous explanation), you can go to http://dinkumware.com/exam/ and try the EDG/C++-compiler. It's considered the absolute top notch with regard to conformance, so if it compiles there, you can be pretty much sure it's intentional.

The declaration in question here is mentioned as an accepted declaration in section 3.1 of the standard. The grammar is probably inherited from C, but you may be able to find it somewhere in annex a (I still haven't read through all of that )



 
 
Simple Samples





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

If you're ever in doubt about such issues, and cannot be bothered to check the standard (or it's difficult to locate an un-ambiguous explanation), you can go to http://dinkumware.com/exam/ and try the EDG/C++-compiler. It's considered the absolute top notch with regard to conformance, so if it compiles there, you can be pretty much sure it's intentional.

I am sorry I was not clear, but I do not doubt that the VC 6 compiler was erroneous. There is no question about that.



 
 
Bapa





PostPosted: Visual C++ Language, This code snippet makes no sense... Top

None of the responces here have cleared up any issues. And I am quite firmiliar with C++, and fluent in a few other languages. It's just this syntax doesn't make much sense to me. I've asked a few of my other programmer buddies, and they don't know either. And please, it's kinda... akward something trying to explain the bare basics to me xD

I have Visual Studio 2005, and my code compiles without error. The someFunction function (duh) takes a "someClass" pointer has a parameter. When calling functions like that, you could either pass in a pointer directly. Or if you don't have one, you can just pass in the address of a regular variable, and inside the function, the pointer then points to the address passed-in. They're both acceptable.

I just don't get it. I've let about 3 of my friends test out this code. It compiles, and then they're just stumped on how it works :\

And it looks like you're running the code under Managed C++. This code was written in native C++ :\

Here's the actual code sample I used:

#pragma comment (lib, "d3d9.lib")

#pragma comment (lib, "d3dx9.lib")

#include <windows.h>

#include <d3d9.h>

#include <d3dx9.h>

#include <iostream>

using namespace std;

#define PAUSE cout << endl; system("pause");

int main()

{


// just a regular matrix
D3DXMATRIX someMatrix;

// call the function, no need to prototype it...
D3DXMatrixIdentity(&someMatrix);

//
D3DXMATRIX *D3DXMatrixIdentity(D3DXMATRIX *pout);

// regular matrix

D3DXMATRIX myMatrix1;

// pointer to matrix

D3DXMATRIX *myMatrix2;

// create new matrix in Free Store

myMatrix2 = new D3DXMATRIX();

// call function with address of matrix 1

D3DXMatrixIdentity(&myMatrix1);

// pass in the matrix pointer

D3DXMatrixIdentity(myMatrix2);

delete myMatrix2;

}


That was a console program. And now that you mention it, that piece of code that I don't get does look like a prototype. But why would I need a prototype for It's already been defined, and it's already useable.

I get a compile error when I redefine it again, which only re-enforces the fact that it's a prototype. But I don't get it's purpose :\

I guess there's something I'm unfirmilar with. I've read about 3.5 books on C++, but nothing seems to have covered this yet. And yes, I do know what prototyping is :P

I apologize fer my poor paragraph structure, but I am bushed.


 
 
einaros





PostPosted: Visual C++ Language, This code snippet makes no sense... Top


And please, it's kinda... akward something trying to explain the bare basics to me xD

The notion that one knows most of the language is often an indication that you really don't see the full extent of it :) Whenever you reach a point where you feel that you know C++, and all it has to offer, I can assure you that there are still areas unexplored.


And it looks like you're running the code under Managed C++. This code was written in native C++ :\

No, this thread has been all about native C++.

Here's the actual code sample I used:
[...]
int main()

{
// just a regular matrix
D3DXMATRIX someMatrix;
// call the function, no need to prototype it...
D3DXMatrixIdentity(&someMatrix);
//
D3DXMATRIX *D3DXMatrixIdentity(D3DXMATRIX *pout);
// regular matrix
D3DXMATRIX myMatrix1;
// pointer to matrix
D3DXMATRIX *myMatrix2;
// create new matrix in Free Store
myMatrix2 = new D3DXMATRIX();
// call function with address of matrix 1
D3DXMatrixIdentity(&myMatrix1);
// pass in the matrix pointer
D3DXMatrixIdentity(myMatrix2);
delete myMatrix2;
}

I'll just go through some of the constructs seen here, and in your original post, with comments:

int myFunction()
{
D3DXMATRIX someMatrix; // defines a variable
D3DXMatrixIdentity(&someMatrix); // calls a function
D3DXMATRIX *D3DXMatrixIdentity(D3DXMATRIX *pout); // (re)declares a function
D3DXMATRIX *someFunction(&someMatrix); // defines and initializes a variable
}


That was a console program. And now that you mention it, that piece of code that I don't get does look like a prototype. But why would I need a prototype for It's already been defined, and it's already useable.

Yes, it's a declaration. When your compiler sees a declaration like that, whether inside or outside a function body, it's assumed to have external linkage, and the compiler will allow you to use the function as it's specified. When the linker comes into play, it will attempt to match your declaration against a function exported from another translation unit or static library.

Imagine an application which consists of two source files, one.cpp and two.cpp, with the following source:

=== one.cpp ===

void foo(int i)
{
// do something with i
}

=== two.cpp ===

int main()
{
void foo(int); // declare the function
foo(10); // call declared function
}

In case of your example above, the redeclaration is likely to be pointless. Perhaps it's just a typo, or something the author used to see which parameters he was supposed to use when calling the function. The compiler happily accepts it, even though it's superfluous.


I guess there's something I'm unfirmilar with. I've read about 3.5 books on C++, but nothing seems to have covered this yet. And yes, I do know what prototyping is :P

I suggest you acquire the ISO/IEC standard document. That's a good read :)