Results 1 to 2 of 2

Thread: passing structs by reference

  1. #1

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258

    passing structs by reference

    I have a class that has some struct types defined publicy:
    Code:
    //.h file
    struct foo
    {
    int a;
    string b;
    char c;
    };
    void test(int a, foo f1);
    Now in the .cpp file, I want to to have a function take a parameter as a type of that struct;
    Code:
    foo f1;
    void test(int a, f1);
    what is the correct way to pass and access this by reference:
    test(1, &f1)
    ...
    test(....)
    {
    *ft.a = 1;
    }

    no!

    I am confused as to how pointers, the * and & operators and everything works with objects.
    Can someone point me to a good tutorial or give me a simple snippet please?

    thx.
    balls deep in bad code

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: passing structs by reference

    Code:
    foo f1;
    void test(int a, f1);
    This is not a correct function declaration, here you declare a variable of type foo, and a function with no type for the last parameter. Function declarations always look like this:
    Code:
    void test(int a, foo f1);
    If you want to use a reference instead you should replace foo by foo& so you get:
    Code:
    void test(int a, foo& f1);
    To use f1 inside that function you can act as if it is a normal vriable, the only difference is that thing you pass as the parameter is modified.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width