Thread: Using += operator to insert data into a dynamic array...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    Using += operator to insert data into a dynamic array...

    hi...

    I need to use a +=operator to insert data into a dynamic array.

    Example:

    Product fields:
    ID: 123BRD
    Type: bread
    Detail: healthy bread
    Cost: 10
    I have a class orders:
    Code:
    class orders
    {
    	string idOrder;
    	string clienteName;
    	products *arrayProd; 
    	unsigned int numProduts;
    	unsigned int quantity;
    	
    	public:
    		...
    I need to use a += operator to insert the products i want to order into the products *arrayProd;

    I now how to do that without a += operator, but i have no idea how to use a operator for this kind of thing.

    I thought that operators were only used for math calculations :-S
    Last edited by IndioDoido; 11-03-2007 at 01:41 PM.
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First, you should use std::vector instead of a raw pointer array. This is C++, not C.
    You can overload operators in classes. Operator += should be like:
    Code:
    my_class& operator += (type_you_want_to_accept_here my_type);
    type_you_want_to_accept_here should be what you're expecting to store.
    Code:
    int my_int;
    my_class classy;
    classy += my_int; // Calls my_class& my_class::operator += (int my_type);
    Then you just do the code for it.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are writing your own dynamic array class? I agree that operator+= is not all that intuitive for inserting into a dynamic array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But yes, do consider adding a method to add data instead of operator +=. Just because you can do it doesn't mean you should.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think the question was answered here except now you are using a raw array instead of a set as the container type.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi Elysia

    this is for a school assignment and i'm not allowed to use vectors.
    i must specifically use dynamic arrays and a += operator for this :-(

    Hey anon. Yes i did open a topic with this issue, sorry if i'm repeating things, but i didn't quite get the answer i needed, and know i need to use the operator for a array :S
    "Artificial Intelligence usually beats natural stupidity."

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As Elysia noted, overloading operator+= is nothing more than having a normal function but with the name operator+=. If it is a member function (and typically it is), it must have exactly one parameter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    but what is the main purpose in a operator?
    for example, what are the advantages on using a +=operator to insert data? and why not a +operator or ++operator?

    i'm really confused, only now i started to work with operators and i'm already hating them :-(
    "Artificial Intelligence usually beats natural stupidity."

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but what is the main purpose in a operator?
    To make certain syntax more "natural", e.g., for a complex number class, or a big number class, or even a string class due to existing convention. In that respect (std::string has operator+ and operator+=) perhaps operator+= and operator+ are viable for a dynamic array container class.

    for example, what are the advantages on using a +=operator to insert data? and why not a +operator or ++operator?
    Using operator++ certainly does not make sense when it come to inserting data. There is no advantage over operator+, just that the semantics are different: operator+= is expected to change the current object, operator+ is expected to return a new object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    thanks for the explanation laserlight ;-)

    going to start working with them...hope they like me :-D
    "Artificial Intelligence usually beats natural stupidity."

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An operator is just an operator. YOU are reponsible for doing the work they should do. But consider this:
    Code:
    my_class classy; // Used to insert data into a dunamic array;
    ++classy;
    classy += NULL;
    Which one of the above statements look most natural to you to insert some data into that array? Not to mention, when other read your work, they may be confused if you use operator ++ (prefix).
    Use them in what you seem to think natural. Their purpose is to make it easier to use classes and make it look more natural.

    And btw, whine at your teacher for not being able to use std::vector There's no sense in using raw arrays (not to mention bad practice) in C++.

    Sample:
    Code:
    Declaration: in .h file:
    class my_class
    {
    	...
    	my_class& operator += (products& product);
    	...
    };
    
    Definition: in .cpp file:
    my_class& my_class::operator += (products& product)
    {
    	// Do your work here. product will be what the user passed.
    	return *this; // Usually operators return a reference to themselves so you can do a = b += c;
    }
    Case you're wondering where product comes from, here's a sample:

    Code:
    my_class classy;
    products my_product;
    classy += my_product; // my_product will be passed to the operator += as the one and only argument
    Last edited by Elysia; 11-03-2007 at 02:47 PM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, the semantics of operator+= are such that it should not modify its argument. Consequently, it should take that argument by const reference:
    Code:
    my_class& operator += (const products& product);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    wow!
    thanks Elysia! really cool examples

    this is my second assignment this year, the first one we used std::vectors, in this second one we must use std::sets,operators and some dynamic arrays :-S

    but don't worry, i'm going to whine at my teacher allloooooottttt :-D
    "Artificial Intelligence usually beats natural stupidity."

  14. #14
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    know this is getting on my nerves

    please...can anyone tel me how do i "transform" this function into a +=operator?

    simple insert product function:
    Code:
    void grocery::insertProduct()
    {
    	string type, detail, prodID;
    	unsigned int cost, weight;
    
    	cout<<"\n Type: ";
    	getline(cin,type);
    
    	...
    
    	...
    
    	products insertProd(prodID, type, detail, weight, cost);
    
    	Prod.insert(insertProd);
    }
    Last edited by IndioDoido; 11-04-2007 at 04:41 AM.
    "Artificial Intelligence usually beats natural stupidity."

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Given that it has no argument, you can't.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you input data from a file into an array?
    By jorgejags in forum C Programming
    Replies: 5
    Last Post: 11-03-2008, 02:48 PM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  4. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2004, 11:01 PM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM