"Selecting" Instances of Classes

This is a discussion on "Selecting" Instances of Classes within the C++ Programming forums, part of the General Programming Boards category; Ok, so I have a class called wpawn (making a chess game) and have instances of wpawn called wpawn1 through ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    18

    "Selecting" Instances of Classes

    Ok, so I have a class called wpawn (making a chess game) and have instances of wpawn called wpawn1 through wpawn8 (8 pawns on a chess board per side). I have a function that can "select" a piece by changing a private data member in the class, "bool selected," from 0 to 1. All other instances should be at 0, so only 1 piece can be selected at a time. Now I wish to manipulate just that one instance of the wpawn class. How do I do this without creating code explicitly for each instance seperately?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    It depends on how your code is set up. One possible answer is to create a function that takes a wpawn reference (or pointer, but reference is better). Pass the appropriate wpawn instance to the function.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think instead of creating 8 seperate objects.. why not create an array of objects.. that way you can access any instances of the pawn class just by using an array index.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    18
    Ahh! An array! Why didn't I think of that? yes, that would solve the problem.

    Thanks for the help!

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    18
    One quick question, I had a nice constructor for the old set up that took 2 values (x and y coordinates). I'm new to using arrays and classes in conjunction, so how exactly would I initialize the instances in the array without creating new member functions? Just not sure of the syntax or procedure.

    [EDIT]
    If that didn't make sense, lemme reword it:

    How do you initialize an array of instances of a class?
    [/EDIT]
    Last edited by SnS CEO; 08-03-2005 at 07:28 PM.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    You can only use the default constructor for arrays of classes, provide some sort of 'set' function.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    18
    UPDATE:

    I have narrowed down my problem to using class arrays as function inputs. So...how do you use class arrays as function inputs?

    also, to Orbitz: http://cplus.about.com/od/beginnerct.../aa073002c.htm

    I followed his model and now I can ditch that ugly initialization function!
    Last edited by SnS CEO; 08-03-2005 at 08:57 PM.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    "class wpawn[8]" ? Don't you want to name your variable something? The 'class' is also not required in C++. Why not use a loop by the way?

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    18

    Lightbulb

    yea, ignore that earlier post, I completely replaced it.

    you also bring up a good point. The name of the instance and the name of the class were the same. I have changed that now so that the class is wpawn and the array is whitepawn[8]. Sorry for the confusion.

    [EDIT]
    THANKS ORBITZ! your second sentence made me realize that I shouldn't be using "class" as the identifier, but rather the name of that class, wpawn. so now it reads:

    Code:
    void findpiece(wpawn whitepawn[8], int x, int y);
    instead of

    Code:
    void findpiece(class whitepawn[8], int x, int y);
    Thanks again!
    Last edited by SnS CEO; 08-03-2005 at 09:04 PM.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    the 8 is also completely useless. wpan whitepan[8] == wpawn whitepawn[] == wpawn *whitepawn in the context of *function parameters*. The C++ Tutorial you listed does not seem very standards compliant to me. I am too lazy right now the check the C++ standard but I doubt what it says works in strict mode. If you use g++ you should always compile with -W -Wall -ansi -pedantic. I do not know how to do the equivalent in VC++

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    18
    hmm... I already changed my code over to that tut's format. I'm using VC++ 6.0. what's g++? I'm very new to advanced compiling.

    and, strangely, it seems like only the 1st 3rd 5th and 7th pawns are being made!

    Here's that new format I'm using from the site I was saying earlier:

    Code:
    wpawn whitepawn[9]={wpawn(1,2),wpawn(2,2),wpawn(3,2),wpawn(4,2),wpawn(5,2),wpawn(6,2),wpawn(7,2),wpawn(8,2)};
    Last edited by SnS CEO; 08-03-2005 at 11:01 PM.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    18
    bump...

    does anyone know why my program doesn't create all 8 instances, only the 1st 3rd 5th and 7th?

    also:

    Code:
    xcoord = whitepawn[looper2].givexposition;
    ycoord = whitepawn[looper2].giveyposition;
    givexposition returns an int, and xcoord is type int, yet it gives me the following error:

    C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\SnSChess\main.cpp(238) : error C2440: '=' : cannot convert from 'int (__thiscall chesspiece::*)(void)' to 'int'
    Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
    C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\SnSChess\main.cpp(239) : error C2440: '=' : cannot convert from 'int (__thiscall chesspiece::*)(void)' to 'int'
    Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast

    [EDIT]

    fixed it :P... I was forgetting the () after the function
    Last edited by SnS CEO; 08-04-2005 at 07:47 PM.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    givexposittion is a function, it would probably help to call it.

    If you have an array then it has created all of them. Please show us how you discovered it only creates the odd values.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of classes?
    By dream_noir in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2009, 11:43 AM
  2. Classes storing instances of own class.
    By HIM in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2006, 08:18 AM
  3. Classes and Destroyed Instances
    By FWGaming in forum C++ Programming
    Replies: 3
    Last Post: 07-12-2005, 10:02 PM
  4. Can not reach the classes and create instances of them
    By kromozom in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2005, 07:31 AM
  5. Instances of classes
    By JasonLikesJava in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 12:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21