Printing array of one view controller in another - nsarray

Can we print an array that is declared in one view controller class in another?
If so how can we do it?
I had tried on it several ways by importing the view controller,used NSLog... but couldn't do so,please do help me.

If you want an array of class1 to print in class2, do these.
Set array as a property of class1 and synthesize it.
Make sure you allocate it and add data to it.
Reference class1 in your class2 and create an object for class1.
class1 *object1;
object1.array should now point to that array.
Note:
You will need to NSLog quite often and check if the array does not become null when you move to a different view controller and make sure you allocate it properly. Also, if possible allocate that array in your AppDelegate and then using the delegate property, delegate.array you will definitely be able to access it.

One way of approaching it can be declare the delegate of the view controller from which u want to pass the array. Now include the delegate in the other view Controller and call its delegate function. Pass the array u want to print in its delegate function.

Related

Basic C++ dynamic allocation issue

There is some basic facet of this that I'm completely missing. I have an object containing a set
PropertyContainer::PropertyContainer(string comFile, string resFile)
{
set<Property*>* prCont = new set<Property*>();
...
}
In my main I'm calling
PropertyContainer* ptrPropertySet = new PropertyContainer(comFile, resFile);
This constructor goes through and populates the set with the data from the files.
If (while i'm still in the constructor) I iterate through the set all the Properties are there.
If I go back to main() and access the set it is empty (I've done it both through an accessor function and temporarily made it public and accessed it directly, to confirm i hadn't made a mistake in the accessor function)
This leads me to believe there is a very basic tenet of programming I'm missing. Any help or links would be appreciated.
You're defining a local variable prCont, which hides your member variable.
It's very rare to dynamically allocate std::sets, and you probably shouldn't.
If you declare the member as set<Property*> prCont, it will be default-constructed automatically when your PropertyContainer is created.

Store objects with different types that inherit the same class in a container

I have the following classes:
class ActivityItem : public QGraphicsItem;
class DeadTimeItem : public QGraphicsItem;
I am at a point where I need to store both objects of type ActivityItem and DeadTimeItem in the same container so I can work with them together.
What have I tried?
I tried creating a QVector that stores QGraphicsItem type, but I got the result that it cannot store abstract class types. What should I do?
My plan
My plan is to store these elements into QVector<T> member of a class timeline, because they will create a timeline. For example:
class timeline : public QGraphicsScene // is this a good idea? to inherit QGraphicsScene?
{
private:
QVector<QGraphicsItem*> items;
int left_offset; // this is for putting the items next to each other
public:
timeline();
void add_item( QGraphicsItem *item );
~timeline();
}
My timeline should look like the sketch below:
So, when the user calls add_item() method,
Timeline *timeline;
timeline->add_item( new ActivityItem( /* parameters */ ) ); // timeline->left_offset += 150;
timeline->add_item( new DeadTimeItem( /* parameters */ ) ); // timeline->left_offset += 100;
the left_offset increases depending on the type of the inserted item.
I know my post is quite long, but please read it completely, because I really need your help! Thank you!
As you've already guessed, I am using Qt. I can also use boost if it is really necessary.
Assuming
QGraphicsItem objects are part of the same scene and have a parent
The list is tightly tied to the scene (member variable of the same class which owns the scene, or member variable of a scene subclass).
Then you should use this:
QList<QGraphicsItem*> items; // or QVector or std::list, nearly same
These are raw pointers, they will not automatically delete the object when removed from list, nor will they get notified when item is deleted. Still, I don't know of a smart pointer type, which would work with QGraphicsItem, so raw pointers are what you must use. So if you need to also remove items dynamically, read on:
First of all, when you delete item, you need to also remove the pointer from this list, to avoid having dangling pointer. As an example, you may want to use something like QList::indexOf() and QList::removeAt() methods for this (also, if removal is very common and there are many items in the list, you may need to rethink the approach, as removing from an big array is slow). You need to be careful that item doesn't get deleted by some Qt code without you having a chance to remove it from the list.
Things get more complicated, if you may have items in the list, which also have their children in the list. If you just delete an item, it will delete it's children, so if your list has pointers to these and your list will end up with dangling pointers to those children. So, you need to do deletion very carefully. There are at least 3 different ways to go about this I can think of (recursive deletion in your own code, removing from list in item destructors, using event filter to catch item removals), so I can expand the answer, if this situation can happen in your code.
Final note: when the entire scene is deleted, it will delete all its children. Only thing you need to do here is make sure the pointers to deleted items in the list don't ever get used, for example simply by clearing the list.
You cannot create an instance of an abstract class, which is fortunate, because trying to assign a derived class to a base class provokes slicing or misbehaves in other interesting ways.
Due to the above restriction, no standard container for base can store a derived. You can circumvent this limitation by using pointers to base, either raw (non-owning) pointers or some smart-pointer type.
The answer using the standard library would be something like:
std::vector<std::shared_pointer<QGraphicsItem>> v;
Using QT classes:
QVector<QSharedPointer<QGraphicsItem>> qv;

passing a vector member variable by reference to another class method

NOTE: I use pseudocode in my question
lets say i have a class called circle with an interface called
circle.h which
i also have a method called readdata but this is defined in another
class called rectangle(rectangle.h is the interface )
i want to call the method readdata in my circle class and pass in my
private member variable which is a vector. How can this be done? is it
correct to pass in a PRIVATE member variable by reference
to another class. Isn't this defeating the whole purpose of having
private member variables because now i am giving class rectangle
access to circle class vector variable since i pass it in by reference. Here is how i
do it(psuedocode)
circle.h
private:
vector<struct> vect;
public:
dataread()
circle.cpp
rectangle.h
readdata(vector &)
method dataread() //class method to fill up my struct
{
rectangle::readdata(vect); //i call rectangle readdata method but i
pass in a reference to my memebr variable....is this safe?
}
should i just declare the vector locally(in dataread method) and
assign it to the reference? any help would be greatly appreciated.
Right now it compiles but i have been told this is not good
programming practice
There is nothing wrong with passing references to private members to methods in other classes. For example:
void myClass::myMethod() {
std::copy(myVector1.begin(), myVector1.end(), myVector2.begin());
}
While that doesn't pass a reference to myVector1 directly, it does pass a writable iterator which is just about the same thing. The class is making a request for some object/function to do something with its data. So long as that other object/function only does what it is supposed to do, there's no problem.
I think I get what you are asking. Yes, you can pass a reference to your private data and No, you shouldn't do it. You can pass a const reference so that it can't be modified or pass a new vector with the contents copied. The best thing to figure out is why you need to do it that way, then figure out the best method for getting the data there.
Passing a pointer to a "private memory area" is not necessarily against the encapsulation idea because the object owning that memory area decides whom to allow access to. It doesn't allow "anyone to access it". On the other hand this doesn't look very natural.
You should return a pointer from the readdata method and use it in your circle instance.
At the same time doing this breaks a principle which I, personally, use: the one who allocates memory is responsible for it so it should also be the one frees it when appropriate. Taking this into consideration, it would probably be a good idea to return the actual vector and not a pointer to it (but this means copying a large amount of memory in case you're not using a compiler with "return value optimization").

Assigning Pointer to singleton to object field

I have a singleton (named Context) with ofcourse a getInstance method which returns the static pointer to the single instance.
When using this instance in other classes I've been using the Context::getInstance() function. But since I've been calling the method tens of times in the same controller class I was looking for a more straightforward solution.
Can I make an extra field in my Controller class of type Context* and assign the singleton to it in the constructor of my controller by calling Context::getInstance() once? Or will the pointer itself change during the program lifetime?
If you're using a singleton it should stay the same object for the lifetime of the program, and so this should be safe. If you yourself wrote the class, then you should easily be able to check that this is the case.
it won't if you don't change it
another solution could be:
Context* context()
{ return Context::getInstance(); }
Yes, you can, but your solution is not safe - you can access this extra field before the first call of the getInstance method by mistake. You should better store a pointer returned by getInstance in your "Controller" class somewhere.

Member pointers or reference arguments?

I have the following problem.
I got a class PluginLoader which oversees loading of plugins. It divides sub-stages of work to other classes like Plugin. Plugin calls functions of PluginLoader in its processing. Let's call that function AddData. Here, PluginLoader has to check if the data it receives is duplicate. For that, it uses a ConflictResolver class. Now, my problem is how to make an object of ConflictResolver available to PluginLoader. There are 3 ways I see out of this.
Use a ConflictResolverFactory class and create an object of ConflictResolver for PluginLoader.
Pass a constructed ConflictResolver* to the PluginLoader via its constructor or a member function SetConflictResolver and store it in a member variable and use it later. Both ways have drawbacks. If I pass it in the constructor, I will have to throw if the pointer is NULL. And I can't use exceptions as it is the custom here. If I pass it via SetConflictResolver, there is no way that I can guarantee that that function will be actually called by the user. Or I will have to check whether the member ConflictResolver* is NULL everywhere I use it.
Pass a ConflictResolver & to PluginLoaders Load method where all the work will be done. In turn, Plugins Load method has to accept a ConflictResolver & as well (though it has no use for it) and pass that back to AddData where PluginLoader will be able to use it.
Third method is safer compared to second. However, I have to pass around a reference even when it is not used.
If the first method cannot be used, what is the best way to do this?
Apologies for the wall :wq!
You could pass a ConflictResolver& to the PluginLoader constructor. You can now guarantee that the object is not null.