Thread: Getting Struct Variables

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    33

    Getting Struct Variables

    I've tried posting this before, but I wasn't very clear.

    I have two cpp files, and two headers.

    both the cpp files include the headers of the other cpp files, so every function in each cpp can be used by the other cpp.

    In one header, I made a struct with a floating point in it.

    I gave the floating point a value in cpp file number 1.

    I want to access that floating opint in cpp file number 2, but how do I tell it where to find the value?

    I defined a struct of the same type in cpp number 2, but I need to give struct.float a value, and that value has to be the same as the one in cpp number 1.
    Last edited by Muphin; 09-04-2005 at 11:56 PM.

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    It sounds like you want a global variable:
    Code:
    // header1.h
    #ifndef HEADER1_H
    #define HEADER1_H
    
    struct test {
      double f;
    };
    
    extern test t;
    
    #endif
    Code:
    // header2.h
    #ifndef HEADER2_H
    #define HEADER2_H
    
    void foo();
    
    #endif
    Code:
    // header1.cpp
    #include "header1.h"
    
    test t = {0};
    Code:
    // header2.cpp
    #include "header1.h"
    #include "header2.h"
    
    void foo() {
      t.f = 1.5;
    }
    Code:
    // main.cpp
    #include <iostream>
    #include "header1.h"
    #include "header2.h"
    
    int main() {
      foo();
      std::cout << t.f << '\n';
      return 0;
    }
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    That isnt gonna work, I need cpp #2 to go through every struct that was made in cpp #1 (there will be a lot) and read it's float value. If I extern them, I'd have to manually type in the name of the struct, and the code would just be long and disorganized.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Source #1 calls function from Source #2 and passes the structure as a parameter. And vica versa.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    Quote Originally Posted by Thantos
    Source #1 calls function from Source #2 and passes the structure as a parameter. And vica versa.
    Yes, I could do this, but then I'd have to call the function hundreds of times, for every struct I have.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Then you have a fundemental flaw in your program design.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    Quote Originally Posted by Thantos
    Then you have a fundemental flaw in your program design.
    How so? all my directX frames need a name of their own. (the frame variable is inside the struct, plus a few values that I need to get.)

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    I was discussing this with a friend who knows a little bit of C++.

    He says I should use classes, not structs. I think its a good idea.

    One thing he suggested is an accesser function, he says it'll return the value of a private variable defined in the class.

    I pointed out a problem, my cpp #2 needs to go through every class in a for loop, but I can't have the classes in an array, because I will have so many, they need names for me to keep track.

    So he suggested using a collection, or at least thats what he thinks they're called. But he had never used a collection, so he couldn't help me with that.

    Is a collection a good idea? Should I bother to use classes? Will an accesser function work?

  9. #9
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    He says I should use classes, not structs.
    Structures are classes with default public access. Your friend isn't making any sense. Changing the struct to a class wouldn't make any difference because you can use a struct like a class. Then again, he could have some irrational idea of what a class is. Some people are like that.
    One thing he suggested is an accesser function, he says it'll return the value of a private variable defined in the class.
    That's a better design, but basically the same thing as Thantos' suggestion that you didn't like. You still have to call a function hundreds of times.
    but I can't have the classes in an array, because I will have so many, they need names for me to keep track.
    Why do they have to have names? What's wrong with a unique index that's so hard to remember for you?
    So he suggested using a collection, or at least thats what he thinks they're called.
    He was--hopefully--thinking of a map if the problem is assigning a name to each object.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Replies: 4
    Last Post: 12-12-2002, 02:32 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM