im newbie in c++ so I hope you can help me in this.
i have this class Appearances, i show a bit of the code of the cpp
Appearances::Appearances(const char* id, float shininess,const char* textureref)
{this->id = id;
setShininess(shininess);
this->textureref = textureref;
}
and i want to join another class "Component" like this
Component(float ambient[4] , float diffuse[4] , float specular[4])
{setAmbient(ambient);
setDiffuse(diffuse);
setSpecular(specular);
}
And what I want is that i can call appearances with all of this joined, for example:
app = new Appearances(idAppearance, vAmb, vDif, vSpec, shininess, txtRef);
im trying to get this on c++
<appearance id="app1" shininess="6.0" textureref="ss" >
<component type="ambient" value="5 5 5 5" />
<component type="diffuse" value="5 5 5 5" />
<component type="specular" value="0.6 0.6 0.6 0.6" />
</appearance>
I dont know if I explained well what i want, but can someone help me? :)
To do that you would need to change your constructor to include the data of the component class.
Appearances::Appearances(const char* id,float ambient[4] , float diffuse[4] , float specular[4], float shininess,const char* textureref)
Then in the constructor you call the constructor of Component.
But this raise the question of design. If you can initialize your component, it means it just a logical group you want to do, i suggest to use nomenclature instead like comp_Ambient.
If you want to be able to have more than one component in your class, then you don't want to initialize it in the constructor.
The other reason I see to use this design, is being able to use component on other object, then, don't put it in the class.
Do you need full access to components members and functions? If so, I see two possible answers to your question. The simplest way would be to create a derived class which inherits from both Appearances and Component. For example, you could declare class Design: public Appearances, public Component. While multiple inheritance can sometimes complicate design, it seems to be the most straightforward option. See more here.
If you would like to add the functionality to the existing Appearances class, this can be done by making Component a friend class of Appearances. This is done by adding the line friend class Appearances; to your Component class, which allows Appearances to access all members of Component. In this case, you will also need to declare two constructors: one for initializing Appearances without Component, and one for initializing Appearances with the data for Component.
Related
I have 4 classes, Customer, PickyCustomer, SegmentCustomer, and Delivery.
The Delivery class has a component within it called customer, like so:
class Delivery {
private:
Customer *customer;
Delivery(Customer *cust) // constructor
each of the Customer classes have a method called getAcceptable()
that is overrideable and is overridden within PickyCustomer and SegmentCustomer
From what I've read this is the correct way to call the overridden method from the Delivery class. By keeping the customer as a pointer it allows Delivery to call the child methods that override the base class.
But when trying to use unique_ptr I keep getting the error
no matching function for call to 'Delivery::Delivery(std::unique_ptr<Customer>*)'
when using the following bit of code to initialize
unique_ptr<Customer> cust1(new Customer("Name", "Home", 1000.0, 25.0, fileProduce));
shared_ptr<Delivery> cust1Delivery(new Delivery(cust1));
Can someone explain why I am getting this error, and show how I could fix this? Thanks.
Thanks to #paddy in the comments with the help! Using the get() function worked for me.
The below works for me.
unique_ptr<Delivery> cust1Delivery(new Delivery(cust1.get()));
Developing a Drupal 8 example site, I have declared block in a module, and I want to do a few things with this block, like check the route and show this block only on nodes, also check if the user has permissions to see this block, and the content of the block is a form which I had defined in another place of the module.
I don't want to get the classes/services that I need in a static way, I want to use dependency injection to get those classes because it is technically better to decouple code and allow better testing.
Now "create" method and the "constructor" method on the block are like so:
<?php
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_user'),
$container->get('form_builder'),
$container->get('current_route_match'),
$container->get('access_check.permission')
);
}
public function __construct(
array $configuration, $plugin_id,
$plugin_definition,
AccountProxyInterface $user,
FormBuilderInterface $formBuilder,
ResettableStackedRouteMatchInterface $route,
AccessInterface $access
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->user = $user;
$this->formBuilder = $formBuilder;
$this->route = $route;
$this->access = $access;
}
Is this the correct way to do this? Maybe I'm doing too much in the block file? Should I create a service to move the logic to another place? Probably I would need more things, which means using more services, and my "create" and "constructor" methods are growing in parameters. Is this the correct way to do it? Thanks.
When you have to inject many services in one class, be it a controller or a block, it usually tells that the class is not well designed because you are (probably) trying lots of things in just one class.
However, I've seen many controllers which inject multiple services in their constructors, so it doesn't seem an unusual practice anyway. "Every rule has an exception".
In the end, I think it is a matter of balance, build a class that is responsible for doing one logical thing, and its dependencies in the same way.
I'm new to the site (and to c++) so please forgive me if this is a basic question - I've googled and looked through this site without success so far, so any help anyone can provide would be hugely appreciated.
I'd like to add some functionality to an app, that allows a user to fully define the structure and contents of an object. For example, user would be presented with a configuration screen that allows them to list each property of the object - given my limited knowledge I've assumed this might be achieved by using a class:
Class Name: CustomClassName
Class Property 1: property1Name property1DataType property1DefaultValue
...
Class Property n: propertynName propertynDataType propertynDefaultValue
The user would then be able to hit a button to save their custom configuration, and the program could then reference that configuration as a Class:
class CustomClassName
{
property1DataType property1Name = property1DefaultValue;
...
propertynDataType propertynName = propertynDefaultValue;
}
I'm not even sure this is possible using Classes, so if there's another mechanism that facilitates this I'm open to suggestions!
You can't create classes in runtime, but since dynamic typing is in essence a subset of static typing, you can fake it.
Start with the Property type1:
using Property = variant<int, float, string>;
A simple "dynamic" class could look like this:
class DynamicClass {
std::map<std::string, Property> properties;
public:
Property const& operator[](std::string const&) const
Property operator[](std::string const&);
};
Use:
DynamicClass d;
d["myInt"] = 5;
1 Example implementation. Internals of variant should be tailored for your specific purpose. If you need an open variant, where you don't know all of the possible types beforehand, this gets more complicated, calling for something like any.
I have created a pass that pathprofiles and then stores results in different data structures such as blocks corresponding to the paths, edges in paths etc.
I have different variables and data structures for each of these.
Is there a way to use these variables directly in another pass that i write?
If yes, how? (Im not sure if getAnalysisUsage works for this?)
Urgent help required
This answer might be late, but I had the same question, ran across your post and thanks to Oak was pointed into the right direction. So I wanted to share some code here.
Suppose you have two passes, the first one is your PathProfilePass and the second one is your DoSomethingPass. The first pass contains the data that you collect and share with the second path; nothing special needs to be done here:
/// Path profiling to gather heaps of data.
class PathProfilePass: public llvm::ModulePass {
public:
virtual bool runOnModule(llvm::Module &M) {
// Create goodness for edges and paths.
...
}
std::set<Edges> edges; ///< All the edges this pass collects.
std::set<Paths> paths; ///< All the paths this pass collects.
};
The interesting stuff happens in the second pass. Two things you need to do here:
Specify the dependency of the second pass on the first pass: see the getAnalysisUsage method.
Access the data from the first pass: see the getAnalysis method.
Code-wise it would look something like this for the second pass:
/// Doing something with edge and path informations.
class DoSomethingPass: public llvm::ModulePass {
public:
/// Specify the dependency of this pass on PathProfilePass.
virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const {
AU.addRequired<PathProfilePass>();
}
/// Use the data of the PathProfilePass.
virtual bool runOnModule(llvm::Module &M) {
PathProfilePass &PPP = getAnalysis<PathProfilePass>();
// Get the edges and paths from the first pass.
std::set<Edges> &edges = PPP.edges;
std::set<Paths> &paths = PPP.paths;
// Now you can noodle over that data.
...
}
};
Disclaimer: I haven't compiled this code, but this is an adaptation to your example of what works for me. Hope this is useful :-)
Set a dependency from the 2nd pass to the 1st pass (via overriding getAnalysisUsage and invoking getAnalysis - see the programmer's guide to writing a pass on how to do that). Once you get an instance of the 1st pass, you can use it just like any other C++ object.
I want to create a contact listener in such a way that I'll be able to create a joint when sprites from different classes meet. I found a useful question and answer which has helped me - Getting the world's contactListener in Box2D partly. The following code and insrtuctions where recommended:
std::vector< std::pair<b2Fixture*, b2Fixture*> > thingsThatTouched;
//in BeginContact
thingsThatTouched.push_back( make_pair(contact->GetFixtureA(), contact->GetFixtureB()) );
//after the time step
for (int i = 0; i < thingsThatTouched.size(); i++) {
b2Fixture* fixtureA = thingsThatTouched[i].first;
b2Fixture* fixtureB = thingsThatTouched[i].second;
// ... do something clever ...
}
thingsThatTouched.clear(); //important!!
For this to work you'll need to make the thingsThatTouched list visible from the contact listener function, so it could either be a global variable, or you could set a pointer to it in the contact listener class, or maybe have a global function that returns a pointer to the list.
I am using Cocos2d and don't know much C++. How can I "make the thingsThatTouched list visible from the contact listener function" in Cocos2d? Should
std::vector< std::pair<b2Fixture*, b2Fixture*> > thingsThatTouched;
be in the ContactListener.h file? How will it differ in Cocos2d? Thanks.
Put this in a header file:
typedef std::pair<b2Fixture*, b2Fixture*> fixturePair;
typedef std::vector<fixturePair> fixturePairVector;
extern fixturePairVector g_touchingFixtures;
Then include the header wherever you need to use the list. You will also need to have this in a source file (.mm or .cpp) somewhere, just once:
fixturePairVector g_touchingFixtures;
Of cause, the typedefs are not necessary but they may help if you don't like looking at too many of the wrong kind of brackets.
You could store this list in a singleton class, then you can access it from anywhere, even C++ code. Something like that:
NSArray* things = [SomeSingleton sharedSingleton].thingsThatTouched;