Classes question...

This is a discussion on Classes question... within the C++ Programming forums, part of the General Programming Boards category; geeze this stuff is complicated. Ok, I know this may be elementary but assignment operator is what exactly. ( sorry ...

  1. #16
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    geeze this stuff is complicated. Ok, I know this may be elementary but assignment operator is what exactly. ( sorry it is 1 in the morning and I have school, but i dont want to quit working till I get this figured out. Thanks for any assistance

  2. #17
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    You'd essentially need something like this:

    Code:
    class anint {
    private:
        int *i;
    public:
        anint(int i) { this->i = new int; this->i = i; }
        int get() { return *i; }
        ~anint() { delete i; }
    
        anint(const anint& other){ i = new int; *i = *(other.i); }
        anint& operator=(const anint& other){ *i = *(other.i); return *this;}
    
    };
    Essentially you need to implement those last two so it is copy-by-value; if you don't, you could end up with many objects pointing to the same memory location.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #18
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    oh, ok thank you, well I am gunna gt some rest now, I will try and get it working during free time at school. Ill probably post again here is around 15 hours or so... well thanks again for the help everyone. Also Cat I will see how well I can implement that design, yet again thank you all

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    However, the anint example is different from the suggestion of using vectors to hold pointers and making sure they are deleted. For the vector of shared_ptr's solution, you just have to download the latest boost (you don't have to build it), then do something like this:
    Code:
    class data_manager
    {
      // No copy constructor needed, no copy assignment needed, no destructor needed.
    
      // probably better ways exist, this is an example.
      void AddNew(data_arc* new_data)
      {
        data_list.push_back(boost::shared_ptr<data_arc>(new_data));
      }
    
    private:
      std::vector<boost::shared_ptr<data_arc> > data_list;
    };
    With a vector of raw pointers, you have to do only a little more work:
    Code:
    class data_manager
    {
      // No copy constructor needed, no copy assignment needed, but destructor is necessary.
      ~data_manager()
      {
        // Loop through vector calling delete on each pointer.
      }
    
      // probably better ways exist, this is an example.
      void AddNew(data_arc* new_data)
      {
        data_list.push_back(new_data);
      }
    
    private:
      std::vector<data_arc*> data_list;
    };

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Cat
    Code:
        anint(const anint& other){ i = new int; *i = *(other.i); }
        anint& operator=(const anint& other){ *i = *(other.i); return *this;}
    Bad idea when dealing with pointers to polymorphic classes. You'll get slicing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    May 2005
    Posts
    6
    alright i dont even care about noob flaming, just can someone please define vector, containment class, and basically everything in this thread? i used to be capable in playing around with C++ but it's been a long time, and i never got into object oriented programming. I came on here with essentially the same question as original poster, but everything in here is way above me. My current goal (i used to set programming goals to figure out new parts of the language), is to create a simple genetic structure (population genetics, ecosystem genetics) model, which at simplest would caluculate gene and allele frequencies of one species through simple varying enviornment (ie, hot weather/cold weather)

    and pls dont refer me to read 30 hours of tutorials and books, as i dont have that much spare time. I think many of us are the same, but i learn a lot better with things like this from examples and direct explainations, then experimentation.

    thanks in advance for tolerating the impatient noobness

    edit: to avoid going off topic, one could also PM me with email and whatnot, that would be awsome

  7. #22
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Much of what programing is about is manipulation of data. Each piece of data is considered a single variable. Often times you want more than a single variable in your program and many times the variables represent closely related entities so rather than having x number of named variables you can hold all of them in a container of some sort. The simplest containers are arrays and lists, but there are any number of other containers such as queues, stacks, trees, sets, maps, etc. Each type of container can be written using stand alone functions to manipulate the data contained within the container, but often the functions to manipulate the container is enclosed in a class. You can write your own container class or use one of the common container classes written in the Standard Template Library. The STL vector class is basically a souped up version of an array that expands as needed so you don't have to do the memory management on your own. There are some other nice features with vectors as well and there are still some problems; for example, there is no bounds checking in the vector class, you still need to do that.

    If you write your own class it needs to have at least four things. It needs to have a default constructor, a copy constructor, an assignment operator and a destructor. In fact, if you don't implement your own version of these methods, the compiler will create a version for you, which may or may not do what you want done.

    The discussion in this thread has been quite broadly based. Sorry, there's no simple/easy way to learn about these things. I'd encourage you to work through the pertinent sections of the tutorial on this site to learn more about any of the individual topics, and if you don't have a textbook handy, I'd encourage you to get one of those, too.
    You're only born perfect.

  8. #23
    Registered User
    Join Date
    May 2005
    Posts
    6
    alright thank you

  9. #24
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    very well, I have a few books, and I read each daily usually, and Idit ... ( wow... ).... anyways, talk about asking to much!!! Anyways thank you, once I get a verified working version of my code I will post it, and let you guys see what you think. Thanks again for all the help

  10. #25
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Idit, if you want help go start your own thread; stop hijacking. And if you don't want to sit down and spend some time reading tutorials and books, fine. Good luck with that.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with winAPI in classes (newbie question)
    By Spyril in forum Windows Programming
    Replies: 3
    Last Post: 11-09-2007, 02:21 PM
  2. Replies: 2
    Last Post: 07-28-2006, 02:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 02:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 07:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM

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