Use variable of a constructor - c++

I am not a C++ expert hence I will try to make this as clear as possible, do not hesitate to ask me about any detail you would need.
I am using a program to handle ply file.
I am writing my code in MyFunction.cpp, and calling a function from AnotherFunction.cpp on my file like this : MeshFunction(myplyfile) (MeshFunction definition is in the file AnotherFunction.cpp).
Let's say that MeshFunction is using a vector of elements at a certain point and that I would like to get it in order to use it back in MyFunction.cpp; how is that possible ?
Thank you very much!
Yours faithfully,
L

Update the interface of MeshFunction to accept std::vector<Element>& elements as another arugment.
Make sure to fill elements with the necessary data in the implementation of MeshFunction.
Supply the argument when calling the function.

Related

A C++ program with user-defined variables

I'm writing a code that implements simple command line calculator, and I wanted to add a function for user to create his own variables with numeric values for better calculations. Can anyone share any advice, what technique should I use? I thought about creating an allocated two-dimensional string array and later just converting numeric values or use a template, something like that:
template<class T>
void UserVariables(T varname, T varvalue){
T tArray[2][MAX_VARS]={ {varname1, varvalue1}, {varname2, varvalue2}, ... };
....
}
Maybe there are any better and less noobie-ish decisions out there? Thanks in advance.
Your options are here:
std::unordered_map
std::map
std::unordered_multimap
std::multimap
I don't see how your attempt would work - templates are evaluated at compile time, not run time, so the user can't make new variables one the code is running.

How can I access methods/functions from another file in this way?

I am new to C++, so I would like to store a file with some methods inside that does some calculations and then I would like to call them like this, FileName.MethodName(Arguments). Is it possible to use the (.) DOT?
I am using C++ in Visual Studio.
You're thinking the wrong way. You don't want to call member functions on a file. You should use a class that wraps around the file stream which has member functions to perform the correct operations on it. Maybe if you can tell us what you're trying to do, I can better suit my answer to your intentions.

Passing variable amount of objects as reference to a class

I have library that im using to load images. The library makes class objects out of the files and has their own drawing functions. Im trying to build a class that can handle these objects and implement some simple logic once im done loading my files. What would be a good way to pass them as reference or pointer to my handling class?
This is what i got so far:
1) Passing them as reference one by one. When i add a file i will always have to write the new file to my class definition.
2) Loading all the objects into array and pass that as reference. I still have to set the class defition so that i know how many items are in the array. But what if i have different amount files to load at various steps of my program? Writing many constructor/function overloads doesnt seem like a right way to do it.
I searched for the weekend and didnt really find any solutions to this so any points and tips would be welcome. Oh and im pretty new to C++ so consider my knowledge limited. I can grasp the idea what the pointers and references are but implementing things with them are slow.
So to repeat my question. What would be a good way to pass variable amount of objects as reference or pointer to my handling class?
you can make your function look like this:
void my_function(std::vector<SomeObject *> &objs) {
// ...
}
and you can then iterate over the objs vector using an Iterator, or just check
objs.size()
see http://en.cppreference.com/w/cpp/container/vector
You did not include source code details about your issue but your solution # 2 should be fine and you can just pass an int variable to hold the number of elements in the array.

C++: use of “().” and “()[].”

I am trying to understand the programming of Siemens scanner using C++ and given that my C++ skills are limited, I am having problems in understanding many parts of the code provided by the vendor.
Problem 1
For instance, the code uses reference (rMrProt) to object MrProt and notations (such as the use of use of (). and ()[].) are very confusing to me.
For instance:
ImageSamples = rMrProt.kSpace().baseResolution()
ImageSize = rMrProt.sliceSeries()[0].readoutFOV()
Some explanation of these statements would be appreciated.
All information regarding object MrProt are in “MrProt.h”, “MrProt.dll”, “MrProt.lib”. All these files have been shared at:
https://docs.google.com/open?id=0B0Ah9soYnrlIYWZkNDU2M2EtYTNmNC00YTc5LTllMzItYzIyMWU4M2ZhY2Fi
Problem 2
Also, I have been trying to read MrProt.dll and MrProt.lib without any success. Only now, I came to know of dumpbin. Any help would be appreciated.
Problem 3
Another confusion that I have is related to some part of MrProt.h itself. There is a statement in MrProt.h:
class __IMP_EXP MrProt: public MrProtocolData::MrProtDataDelegate
{
typedef MrProtocolData::MrProtDataDelegate BasicImplementation;
public:
MrProt();
MrProt(const MrProt& rSource);
…
….
}
Here, __IMP_EXP, I guess that it’s some compiler specific stuff.. some decoration etc. But, I still have no idea what to make of this.
Problem 1.
rMrProt.sliceSeries()[0].readoutFOV()
means
Take rMrProt's sliceSeries member and call that. Apparently, it returns an array-like object, something that can be indexed.
From the result, take the first element ([0]). That's some kind of object.
On that element/object, call readoutFOV.
Problem 2. You're not really supposed to read binary files. There should be documentation with them.
1)
ImageSamples = rMrProt.kSpace().baseResolution()
This is just method chaining. You call the method kSpace() on rMrPrto which returns an object, and you call baseResolution() on that object.
2) Those are binary files. What would you expect to see? To read them you'd have to be an expert in asm or at least know some low-level concepts.
3) __IMP_EXP is a common type of directive that tells the compiler that the class is either exported or imported.
It expands to _declspec(dllimport) or _declspec(dllexport), depending on whether the definition of the class is in the current module or another module.
identifier() is a method/function call
identifier[i] returns the i'th element in an array.
identifier()[i] returns the i'th element of the array returned by identifier()
I can only help on problem 1:
if the return value of rMrProt.kSpace() is a struct. instead of saving it to a struct and then access it's member you can directly access a member of his with rMrProt.kSpace().MemberName
same for rMrProt.sliceSeries() which I guess is returning an array. so rMrProt.sliceSeries()[0] will access the first value in the returning array.

How to generate C++ Dynamic Objects names?

I'd like to generate a number of objects (in C++) based on the amount/number the user enters.
Now I've somewhere heard that it has to be done using pointer tricks, creating a pointer to an array of the Object type required, and then dynamically increasing the size of array ( at runtime ).
Isn't there a workaround of directly using names like
Object1, Object2..... ObjectX
instead of having
Classname *Object[]
and then using the array index to get the object ?
In either case, it'd be great if someone could clarify on the issue.
Thanks !
If you want dynamically-sized array, then use std::vector. You won't be able to resize a built-in array.
If you want to be able to get an object by string name, then you should use std::map, it has an indexer:
std::map<string, Classname> myMap;
myMap["Object1"] = Classname();
Classname newClassname = myMap["Object1"];
So far no-one has explained why your thinking is flawed. C++ is a compiled language, and it goes to great lengths to turn the source program into efficient machine code. For this reason, the names you give variables are available to the program only at compile time, when you turn it from source into an executable file. Afterwards, when you want to create objects dynamically, those kinds of information are no longer available. The program only knows about the machine addresses where operands to machine instructions are located.
No, there isn't. Moreover, you don't need to; use std::vector.
When I began programming 9 years ago I asked myself the same question. The answer is: you can't.
You can indeed use an array and resize it dynamically, however using an stl vector is much easier (once you learn how to use it).
You can not do that because C++ doesn't have an "environment" (reflection) where variables (and metadata) can reside. Moreover, in C++ all variable names are vanished when the code is compiled.
A way to achieve the effect you want is to use a Map where the keys are strings.