Monday, May 24, 2010

C++ question ?

What is the scope of an object off the heap ? Can I return an object pointer to main or will it go out of scope?





Like this





myclass* function()


{


myclass* p=new myclass;


return p;


}

C++ question ?
You can remember it this way: an object that has been created using new (which would, as you wrote, be stored on the heap), has to be disposed of explicitly using delete. All that goes out of scope in your code, is the variable p but it only points to the object, and the address it points to is copied to the variable that is used in the calling function to store the return value in.





So returning a pointer to a new-ly created object is perfectly acceptable. Just don't forget to delete it in the calling function (in your case, apparently, main()).
Reply:Awe man, I have no clue what you just said, but it sounded "programmery"





I'll have to read that PDF





I have no clue but I'll star


Craps Game Source Code in C++?

My problem is I really suck at getting started. We have to use a pointer for the Player class and we have to input/output each player's (up to 10) name, balance, and how much they gained by playing the game (i.e their original balance + total winnings) from/into a txt file. I already know the rules of the game and all that because we did a simpler version of it already, I just dont get how to set up all the classes and how to change the Player class into a pointer type. HELP if you can!

Craps Game Source Code in C++?
I'm not trying to say that you want me to do your project, It's extremely difficult to explain the full scope and implications of the program in a textbox online. A markerboard would help ALOT.





So, you don't have to put it into a pointer, and pointer holds an ADDRESS. So, declare an array of pointers.





then use new to create your "players".





class player


{


//information to make the class


};





int main


{


player* players[10];


int i = 0;


while(i%26lt;9)


{


player[i] = new player;


i++;


}


}





--------------------------------------...


my point here is that you make a pointer totally separate from the player. you make a new player in memory and the pointer says where it is.


Can anyone write following in c language?

Write a function named allocAndInit() that will receive the following parameters: An int containing the number of doubles to create dynamically, and a double that contains the initial value that every item in the new memory will be initialized to. The function returns a pointer to the newly created data. It may not be a void pointer.

Can anyone write following in c language?
double * allocAndInit(int n, double value) {


double *value = (double*)malloc(n*sizeof(double);


return value;


}

lady palm

C++ programming question (1 question)?

ok i have everything done except this question...


looking at book...


no examples..


heres the qauestions...


code wins the best answer...


explain it to me too








Rewrite the following in pointer notation. Use the simplest form, assuming that this call is not part of a loop; in other words, do not include a counter which would reach the zero element:


array[0] -

C++ programming question (1 question)?
overload the - operator


array is already a pointer type


It can be *array-
Reply:ok there rally isn't enough information to answer your question (rewrite the following what?)


but heres some example code to that uses pointers to access an array element.





#define somenumber 10;


int array[ somenumber];





int * ptr =array; // now ptr is pointing at array[0];


int i = 0; // element of array


int x =*( ptr+i); // value of x equals the value of the array[i] for any i


C++ Function Implementation Data Type Question?

I have a class named Book_Inventory that has a class called BookRecord *searchByStockNumber(long stockNum). BookRecord is another class in another function that has various public and private variables.





How would I implement that function? What would that functions return type be? If it is BookRecord as the return type, should it return a pointer or not?





I can post the source somewhere else if it would help.





Any suggestions are welcome! Thank you!

C++ Function Implementation Data Type Question?
searchByStockNumber() returns a pointer to a BookRecord object. There are a couple of important things to think about when implementing this function.





First, you might be tempted to allocate a static BookRecord object in searchByStockNumber() and return a pointer to it each time. The problem with that is that if there are two callers, the first might stash the pointer away to refer to later, then after the second call, the data the first caller has a pointer to will be changed.





Second, if you allocate a new BookRecord in searchByStockNumber() and return that pointer, then somebody else is responsible for freeing it. You need to be careful not to just lose track of that pointer. Consider the following function:





foo(N)


{


BookRecord * pBook = searchByStockNumber(N);


// do more stuff...


}





In the example above, pBook goes out of scope when you exit from foo() so the memory allocated by searchByStockNumber() is lost.





I would consider writing searchByStockNumber() this way:





BOOL searchByStockNumber(long stockNum, BookRecord * pBook)


{


// do your look up here. Put the results in pBook.


// return TRUE if you found a book; FALSE if you did not.


}





(I don't remember if "BOOL" is the type name for a boolean in C++... I work in a cross-platform system that defines a type called "Boolean" that is what I use.)





This way, the caller is responsible for the pointer. The caller could do one of two things (or more, these are the obvious ones):





BookRecord * pBR = new BookRecord;


if (searchByStockNumber(number, pBR))


...


delete pBR;





or





BookRecord BR;


if (searchByStockNumber(number, %26amp;BR))


...





The suggestion to use an STL map is probably a bad one. You could have memory issues depending on the size of your inventory, and searching based on various attributes would be complex and error prone. I'm sure you're using some kind of database, which makes way more sense.





Hope this helps.
Reply:I would use an STL map. The reason is that each stockNum should be unique.





I would change your design. You need a class that represents the books (is that BookRecord?). It has methods for stockNumber, title, etc. Book_Inventory has a member attribute that is the map.





Search yahoo! or Google for C++ STL map to find the implementation.


C++ Programers...HELP?

If anyone can tell me what all this means i will be thankful for a lifetime. i SO do not want to read a 400 page book over it...:





#include %26lt;windows.h%26gt;





/* Declare Windows procedure */


LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);





/* Make the class name into a global variable */


char szClassName[ ] = "WindowsApp";





int WINAPI WinMain (HINSTANCE hThisInstance,


HINSTANCE hPrevInstance,


LPSTR lpszArgument,


int nFunsterStil)





{


HWND hwnd; /* This is the handle for our window */


MSG messages; /* Here messages to the application are saved */


WNDCLASSEX wincl; /* Data structure for the windowclass */





/* The Window structure */


wincl.hInstance = hThisInstance;


wincl.lpszClassName = szClassName;


wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */


wincl.style = CS_DBLCLKS; /* Catch double-clicks */


wincl.cbSize = sizeof (WNDCLASSEX);





/* Use default icon and mouse-pointer */


wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);


wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);


wincl.hCursor = LoadCursor (NULL, IDC_ARROW);


wincl.lpszMenuName = NULL; /* No menu */


wincl.cbClsExtra = 0; /* No extra bytes after the window class */


wincl.cbWndExtra = 0; /* structure or the window instance */


/* Use Windows's default color as the background of the window */


wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;





/* Register the window class, and if it fails quit the program */


if (!RegisterClassEx (%26amp;wincl))


return 0;





/* The class is registered, let's create the program*/


hwnd = CreateWindowEx (


0, /* Extended possibilites for variation */


szClassName, /* Classname */


"First Program", /* Title Text */


WS_OVERLAPPEDWINDOW, /* default window */


CW_USEDEFAULT, /* Windows decides the position */


CW_USEDEFAULT, /* where the window ends up on the screen */


200, /* The programs width */


150, /* and height in pixels */


HWND_DESKTOP, /* The window is a child-window to desktop */


NULL, /* No menu */


hThisInstance, /* Program Instance handler */


NULL /* No Window Creation data */


);





/* Make the window visible on the screen */


ShowWindow (hwnd, nFunsterStil);





/* Run the message loop. It will run until GetMessage() returns 0 */


while (GetMessage (%26amp;messages, NULL, 0, 0))


{


/* Translate virtual-key messages into character messages */


TranslateMessage(%26amp;messages);


/* Send message to WindowProcedure */


DispatchMessage(%26amp;messages);


}





/* The program return-value is 0 - The value that PostQuitMessage() gave */


return messages.wParam;


}








/* This function is called by the Windows function DispatchMessage() */





LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)


{


switch (message) /* handle the messages */


{


case WM_DESTROY:


PostQuitMessage (0); /* send a WM_QUIT to the message queue */


break;


default: /* for messages that we don't deal with */


return DefWindowProc (hwnd, message, wParam, lParam);


}





return 0;


}

C++ Programers...HELP?
Sounds like a bloody excruciating way to open a windows dialogue box with an exit button.


Saturday, May 22, 2010

C++ returning enumeration from function?

How do i go about returning an enumeration from a function?





via global pointer?





i.e





int myFunction()


{


if(x=1)


{


enum value {val1 = 1, val2 = 2}


}


else


{


enum value {val1 = 2, val2 = 3}


}


}





How would I go about passing the enum back to the main function? Thanks!

C++ returning enumeration from function?
enum is not a type by itself, it's a type category. A variable can't be of type enum, it could only be of a specific enum type.





So, your syntax is not valid.


Here is an example:





enum TypeColor


{ RED, BLUE, GREEN, OTHER};





TypeColor GetColor(char* in_colorName)


{


TypeColor VarColor = OTHER;


if (stricmp(in_colorName, "red" == 0))


VarColor = RED


else if (stricmp(in_colorName, "blue" == 0))


VarColor = BLUE


else if (stricmp(in_colorName, "green" == 0))


VarColor = GREEN;


return VarColor;


}





int main()


{


TypeColor myColor = GetColor("green");


}
Reply:Check this out... maybe it will help.





http://www.thescripts.com/forum/thread68...
Reply:When you declare an enumeration, it's like you are declaring a new data type. The caller of the function must have access to the declaration, and the funciton itself needs access to it. In your example, you'd probably declare it globally, before any code needs to use it. Don't declare the enum inside a function.





Here's a rewrite of your code.





//First, declare and define the enum:





enum value {val1 = 1, val2 = 2};





value myFunction(int x) //function returns type 'value'


{


value retval;





if (x == 1) //DOUBLE equals here!!


{


retval = val1;


}


else if (x == 2)


{


retval = val2;


}





//TODO: decide what you want to do if it's neither!!





return retval;


}





The calling code would look like this:





value val = MyFunction(1);

snow of june