0

is there anyway I can pass a argument through a class like below for example.

class cat
{public:
  void dog(int ID, char *value) // int ID I'd like to be the index array it was called from?
  {
    debug(ID, value);
  }
}

cat cats[18];

cats[1].dog("value second arg, first arg auto filled from index array");
9
  • 2
    I don't understand what you want. Rephrase, this doesn't make sense to me. as-is Commented Jul 6, 2013 at 6:01
  • Why are you naming the class and the method the same thing? That makes it much harder to tell what you're trying to do. Commented Jul 6, 2013 at 6:07
  • I want to store an INT named ID within the class that is the passed index array. Commented Jul 6, 2013 at 6:10
  • What do you mean by "pass a argument through a class"? You may be confused about what the concept of a class is. Commented Jul 6, 2013 at 6:15
  • All I am looking to do is pass the first argument of dog from the class array index which in this example is 1. Commented Jul 6, 2013 at 6:16

3 Answers 3

1

I tested this out and it worked well:

#include <vector>

// Forward declaration of the class
class CatArray;

class Cat {
    // This line means that the CatArray class can 
    // access the private members of this class.
    friend class CatArray;

    private:
        static int ID;

    public:
        void dog(const char* value) {
            // Use ID here any way you want.
        }
};

// Static variables need to be defined.
int Cat::ID = 0;

class CatArray {
    private:
        std::vector<Cat> cats;

    public:
        // explicit means that the argument passed to this constructor must 
        // be an unsigned int. The ": cats(size)" part is an initializer 
        // list that initializes the cats vector so that it would have the 
        // specified size.
        explicit CatArray(unsigned int size) : cats(size) {}

        Cat& operator [](unsigned int index) {
            Cat::ID = index;
            return cats[index];
        }
};

Now use it like this:

CatArray cats(18);

cats[1].dog("oh lol hi");

This method works for any number of arrays you'd wish to declare.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use static variable and increment it in constructor, to keep the track of all intances:

#include<iostream>


class cat
{
    int ID;
    static int IDTracker;
public:
    cat();
    void dog(char* value);
};

int cat::IDTracker = 0;
cat::cat()
{
    this->ID = cat::IDTracker;
    cat::IDTracker++;
}
void cat::dog(char *value)
{
    std::cout << value << this->ID;
}


int main()
{
    cat cats[18];
    cats[1].dog("Second instance, with index value: ");
    cats[2].dog("Third instance, with index vlaue: ");

    return 0;
}

10 Comments

I don't really see how this would work, but what I do know is it's illegal to define int's to values in a class.
i think it would work... static member variables have been supported for a long time...
also the ID would forget which cat it is. A member variable should be included for cat that gets assigned the value of the static.
@ojblass To your first comment: you didnt new the cats the constructor is not called - that's wrong. cat cats[18]; allocates and constructs 18 Cat objects.
@vulcanraven I've rolled back your question to its first version. You were misleaded by ojblass' incorrect comment about new - your updated code didn't even compile.
|
0

I am not entirely sure this will work but some variant of it might...

#include <stdio.h>
#include <stdlib.h>

void *foo;

class cat
{
  public:

  void dog(char *value) // int ID I'd like to be the index array it was called from?
  {
    int ID = (this - ((cat *)foo))/(sizeof(cat));
    printf("%d - %s",ID, value);
  }

};


int main ( void )
{
   cat cats[18];
   foo = &(cats);
   cats[1].dog("Fido");
}

2 Comments

I will try in a moment, if it does ill be very happy. I see you understand my question clearly.
That trow's me Exception - iKonroi's DEV (192.168.1.146) An exception occurred on thread 0xF9000004 "". Exception: 0xC0000005 Address: 0x98022378 Parameters: 0x00000000 0x883A8810 Access violation reading memory at 0x883A8810.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.