Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 calling classes
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Clayton




PostPosted: Tue May 30, 2006 9:09 am   Post subject: calling classes

is it possible to call a class from within a class? for example, i have a game where weapons have stats, but these stats are needed for another class for a character that the user uses, so im just wondering if i can call that class from within my Character class or if i have to do it outside the class (obviously inside would be easier as it would allow me to have many instances of the class)
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Tue May 30, 2006 10:37 am   Post subject: (No subject)

Do you mean calling a method of a class? Something like:

code:

class Class1
  export main
  proc main
    put "Class1 main"
  end main
end Class1

class Class2
  export main
  proc main
    var temp : ^Class1
    new Class1, temp
    temp -> main
    put "in Class2 main"
  end main
end Class2

var foo : ^Class2
new Class2, foo

foo -> main


That code is untest! I don't have access to Turing right now, so it's likely buggy here and there...but yes, it is possible to insantiate an object of a Class within another class. I wouldn't be surprised if you could instantiate an object of that same class within a class...though for what reasons, I'm not too sure...
Clayton




PostPosted: Tue May 30, 2006 10:45 am   Post subject: (No subject)

ok thanks thats pretty much exactly what i wanted to know Very Happy
Cervantes




PostPosted: Tue May 30, 2006 3:16 pm   Post subject: (No subject)

Delos wrote:
I wouldn't be surprised if you could instantiate an object of that same class within a class...though for what reasons, I'm not too sure...


Any sort of structure that links to others like it. A node is the classic example, for the use of creating a linked list.

Another example: Say you wanted to make a grid (for something like Minesweeper). You could do this with a 2D array, yes. However, you could also do it with a Cell object that stores an array of neighbour Cells. This approach would be harder to initialize the grid, but it makes working with the grid much easier.

SuperFreak82: You really need to work on your terminology. If not for Delos' post, I would have no clue what you're asking. You don't "call a class". Heck, you don't even call a method of that class. You call a method of an instance of that class.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: