classes question

This is a discussion on classes question within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I have two classes: Function and Function_Definition. The class Function_Definition contains a static member of a vector of Function_Definition pointers. ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    classes question

    I have two classes: Function and Function_Definition. The class Function_Definition contains a static member of a vector of Function_Definition pointers. Every function has a name (in the form of a string), and so do the function_definitions. Every function_definition name is unique.

    How do I connect a function to a function_definition based on the name? I feel like I need a global set of names, but that seems... not elegant.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: classes question

    Originally posted by ygfperson
    I have two classes: Function and Function_Definition. The class Function_Definition contains a static member of a vector of Function_Definition pointers. Every function has a name (in the form of a string), and so do the function_definitions. Every function_definition name is unique.

    How do I connect a function to a function_definition based on the name? I feel like I need a global set of names, but that seems... not elegant.
    Sounds like a job for std::map. You could do std::map<std::string, int>, int being index into the vector of Function_Definition pointers, or you could even map a string to a function pointer if you wanted.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    That's not my issue... my issue is, where do I store this in an OOP-friendly manner? Is it a good idea to store it as a static member of class A and access it from class B? (A and B being arbitrary classes)

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by ygfperson
    That's not my issue... my issue is, where do I store this in an OOP-friendly manner? Is it a good idea to store it as a static member of class A and access it from class B? (A and B being arbitrary classes)
    Ah, sorry... couldn't make out what you were saying. Anyway, yes, a static member of class A sounds like a good solution (possibly make it private or protected, and make B a friend of A)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 02:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 03:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 03:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 08:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 11:17 PM

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