dynamic function calls (hard to title the problem)

First of all: Sorry for the really bad title I wasn't sure what to title it as. I could've named it something like "noob questions" but I figured that would be less appreciated than what it is now. :b


I'm trying to create a new edge and insert it into a queue for each line. I know my getline and infile is all correct it's line 4 that has me stumped. My professor says that the edges all have to be static so using a pointer and "new" operator isn't an option right? If so; how can I create a new static edge to be inserted for each line of values?

Below is my code currently; I've tried a handful of things that don't agree with the compiler.

On a side note: My problem is with my previous course in computer science not the current one. He told us not to ask him syntax and language questions so It's not one I can bring to him or get in trouble for getting an answer to. It's just been a solid year since I've coded and am kind of rusty on the specifics of a lot of things.


1
2
3
4
5
6
7
8
9
10
11
12
Edge edge;
    
while (getline (inputFile, line)){ 
       
 inputFile>>v1>>v2>>w;
       
 edge(v1,v2,w);
       
 edgeHeap.insert(edge);
       
 
    }
Last edited on
What is the member function that is called on line 7?

Tell more about the edgeHeap. It clearly has a insert-function. What do you think that it does?
I know what all the other things do. It's not what is causing the errors. It's the calling of the constructor edge(v1,v2,w). I'm sure of this much. My only issue is making a static edge for the total amount of lines to be read in.
Thank you for answering the first question. You say that you call constructor on line 7 for object 'edge', which was already constructed on line 1.

If you want to keep things as they are, then the Edge needs a member function to change its member variables.
I'm not sure if you're being sarcastic or not but sorry for not answering all questions I just already understand the logistics of the classes for the most part. It was the fact that he told us the edges can't be created dynamically that threw off my main function.

Yes, I was considering this as an option. I just assumed that I was looking over a more subtle way to implement it due to me being out of practice.

Thank you very much for the input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Foo
{
  int x_;
public:
  Foo( ) x_( 0 ) {} // ctor 1

  Foo( int x ) x_( x ) {} // ctor 2

  Foo( const Foo & rhs ) x_( rhs.x_ ) {} // ctor 3

  void setX( int x ) // setter
  { x_ = x; }
};


// other code
  Foo outer;

  while ( cond ) {
    outer.setX( i );
    container.insert( outer ); // i1

    Foo inner( i );
    container.insert( inner ); // i2

    container.insert( Foo( i ) ); // i3
  }
// other code 

'outer' and 'inner' are local stack variables. The lifetime of 'inner' is just one iteration of the loop; each iteration creates a new instance. The 'outer' exists in longer scope, and its state is changed with the setX().

In the 'i3' a Foo object is constructed and then passed to insert(), but the object is an unnamed temporary that exists only during the evaluation of line 26. That is even shorter life than what the 'inner' has.

Line 17 calls the ctor 1, the default constructor. Lines 23 and 26 call the ctor 2.

Which is better, the i1, or one of i2,i3? That depends on how expensive are the constructor and setX(). In Foo they are about as expensive. If the constructor does a lot of work under the hood and the setX merely adjusts values, then the i1 is preferable. One has to also consider side effects. Constructor, setX, and destructor could do something extra, like std::cout << x_ << '\n';, and then it really matters when such things should happen.


Why did I show the ctor 3, the copy constructor? That is where the nature of insert() matters.
If it is insert( Foo ), then parameter is passed by value and ctor 3 is called.
If it is insert( Foo & ), then parameter is passed by reference and ctor 3 is not called.


What does the insert do? We don't actually need to know. Our code operates with "static" variables, making professor happy, but the container's implementation might still use new. Standard containers like std::vector do keep the data in the heap memory, i.e. allocate and deallocate memory dynamically, but in our code they look like "static" objects. It seems quite likely that the edgeHeap does the same. Swept under the carpet. SEP: http://en.wikipedia.org/wiki/Somebody_Else%27s_Problem
Topic archived. No new replies allowed.